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
- Package Eoe.demo;
- Import Com.mapdigit.gis.DigitalMap;
- Import Com.mapdigit.gis.MapPoint;
- Import Com.mapdigit.gis.geometry.GeoLatLng;
- Import Com.mapdigit.gis.service.IIpAddressGeocodingListener;
- Import com.mapdigit.gis.service.IpAddressLocation;
- Import COM.PSTREETS.GISENGINE.R;
- Import com.pstreets.gisengine.SharedMapInstance;
- Import android.app.Activity;
- Import Android.os.Bundle;
- Import Android.view.Menu;
- Import Android.view.MenuInflater;
- Import Android.view.MenuItem;
- Public class Mapipsearch extends Activity implements
- Iipaddressgeocodinglistener {
- @Override
- Public void OnCreate (Bundle savedinstancestate) {
- Super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- }
- @Override
- Public void OnStart () {
- Super.onstart ();
- GEOLATLNG Center = New GEOLATLNG (32.0616667, 118.7777778);
- SharedMapInstance.map.setCenter (center,
- Com.mapdigit.gis.raster.MapType.MICROSOFTCHINA);
- SharedMapInstance.map.setIpAddressGeocodingListener (this);
- }
- @Override
- Public Boolean Oncreateoptionsmenu (Menu menu) {
- Menuinflater inflater = Getmenuinflater ();
- Inflater.inflate (R.menu.mapgeocoding_menu, menu);
- return true;
- }
- @Override
- Public Boolean onoptionsitemselected (MenuItem item) {
- Switch (Item.getitemid ()) {
- Case R.id.findaddress:
- SharedMapInstance.map.getIpLocations ("58.192.32.1");
- return true;
- Default
- Return super.onoptionsitemselected (item);
- }
- }
- @Override
- Public Void-DONE (String query, ipaddresslocation result) {
- if (Result! = null && result.error.length () = = 0
- && result.longitude.length () > 0
- && result.longitude.length () > 0) {
- try {
- MapPoint MapPoint = new MapPoint ();
- String latlng = "[" + Result.longitude + "," + result.latitude+ ", 0]";
- Mappoint.point = DIGITALMAP.FROMSTRINGTOLATLNG (LATLNG);
- Mappoint.setname (result.organization);
- Mappoint.setnote (result.city + "" + result.country);
- SharedMapInstance.map.panTo (Mappoint.point);
- } catch (Exception e) {
- Result.error = "Ip_not_found";
- }
- }
- }
- @Override
- Public void readprogress (int bytes, int. total) {
- }
- }