An example of the latitude and longitude coordinates queried by an IP address in Android

Source: Internet
Author: User

As we all know, according to the IP address can know its location, in Android can also be the IP address of its location, that is, the specific geographical latitude and longitude coordinates.

This article demonstrates how to query geographic latitude and longitude coordinates based on IP address directly in code, and the following example shows that the 58.192.32.1 IP address is queried to the latitude and longitude coordinates (118.777802,32.061699).

Java code
  1. Package Eoe.demo;
  2. Import Com.mapdigit.gis.DigitalMap;
  3. Import Com.mapdigit.gis.MapPoint;
  4. Import Com.mapdigit.gis.geometry.GeoLatLng;
  5. Import Com.mapdigit.gis.service.IIpAddressGeocodingListener;
  6. Import com.mapdigit.gis.service.IpAddressLocation;
  7. Import COM.PSTREETS.GISENGINE.R;
  8. Import com.pstreets.gisengine.SharedMapInstance;
  9. Import android.app.Activity;
  10. Import Android.os.Bundle;
  11. Import Android.view.Menu;
  12. Import Android.view.MenuInflater;
  13. Import Android.view.MenuItem;
  14. Public class Mapipsearch extends Activity implements
  15. Iipaddressgeocodinglistener {
  16. @Override
  17. Public void OnCreate (Bundle savedinstancestate) {
  18. Super.oncreate (savedinstancestate);
  19. Setcontentview (R.layout.main);
  20. }
  21. @Override
  22. Public void OnStart () {
  23. Super.onstart ();
  24. GEOLATLNG Center = New GEOLATLNG (32.0616667, 118.7777778);
  25. SharedMapInstance.map.setCenter (center,
  26. Com.mapdigit.gis.raster.MapType.MICROSOFTCHINA);
  27. SharedMapInstance.map.setIpAddressGeocodingListener (this);
  28. }
  29. @Override
  30. Public Boolean Oncreateoptionsmenu (Menu menu) {
  31. Menuinflater inflater = Getmenuinflater ();
  32. Inflater.inflate (R.menu.mapgeocoding_menu, menu);
  33. return true;
  34. }
  35. @Override
  36. Public Boolean onoptionsitemselected (MenuItem item) {
  37. Switch (Item.getitemid ()) {
  38. Case R.id.findaddress:
  39. SharedMapInstance.map.getIpLocations ("58.192.32.1");
  40. return true;
  41. Default
  42. Return super.onoptionsitemselected (item);
  43. }
  44. }
  45. @Override
  46. Public Void-DONE (String query, ipaddresslocation result) {
  47. if (Result! = null && result.error.length () = = 0
  48. && result.longitude.length () > 0
  49. && result.longitude.length () > 0) {
  50. try {
  51. MapPoint MapPoint = new MapPoint ();
  52. String latlng = "[" + Result.longitude + "," + result.latitude+ ", 0]";
  53. Mappoint.point = DIGITALMAP.FROMSTRINGTOLATLNG (LATLNG);
  54. Mappoint.setname (result.organization);
  55. Mappoint.setnote (result.city + "" + result.country);
  56. SharedMapInstance.map.panTo (Mappoint.point);
  57. } catch (Exception e) {
  58. Result.error = "Ip_not_found";
  59. }
  60. }
  61. }
  62. @Override
  63. Public void readprogress (int bytes, int. total) {
  64. }
  65. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.