You can query the geographical longitude and latitude coordinates of an IP address based on the IP address. For example, in the following example, the IP address 58.192.32.1 is located at 118.777802 and 32.061699, which is the location of Nanjing University.
[Java]
Package com. pstreets. gisengine. 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, 15,
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 ){
// Handle item selection
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. longpolling. length ()> 0
& Result. longpolling. length ()> 0 ){
Try {
MapPoint mapPoint = new MapPoint ();
String latLng = "[" + result. longpolling + "," + 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 ){
}
}
Package com. pstreets. gisengine. 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, 15,
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 ){
// Handle item selection
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. longpolling. length ()> 0
& Result. longpolling. length ()> 0 ){
Try {
MapPoint mapPoint = new MapPoint ();
String latLng = "[" + result. longpolling + "," + 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 ){
}
}
Note: currently, IP address query results are always returned in English, as detailed in the preceding results.
ISP: "China Education and Research Network"
Organization: "Nan Jing University"
Country: "CN"
City: "Nanjing"
If you want to know the geographic name corresponding to the longitude and latitude, you can use the address anti-encoding service.
You can enter 127.0.0.1 to query the local address.
Author: mapdigit