LWUIT引路蜂地图开发示例:IP地址查询

jerry 地图开发 2015年11月26日 收藏

IP地址查询,可以根据IP地址查询到该IP所在的地理经纬度坐标,比如下述例子查询IP 地址58.192.32.1,所在经纬度为118.777802,32.061699,为南京大学所在地。

  1. package com.pstreets.gisengine.demo.lwuit;
  2.  
  3. //--------------------------------- IMPORTS ------------------------------------
  4.  
  5. import com.mapdigit.gis.DigitalMap;
  6. import com.mapdigit.gis.MapPoint;
  7.  
  8. import com.mapdigit.gis.geometry.GeoLatLng;
  9. import com.mapdigit.gis.raster.MapType;
  10. import com.mapdigit.gis.service.IIpAddressGeocodingListener;
  11.  
  12. import com.mapdigit.gis.service.IpAddressLocation;
  13. import com.pstreets.gisengine.demo.MapDemoLWUIT;
  14. import com.sun.lwuit.Command;
  15. import com.sun.lwuit.events.ActionEvent;
  16.  
  17. public class MapIpSearchLWUIT extends MapDemoLWUIT
  18.         implements IIpAddressGeocodingListener{
  19.  
  20.     private Command mapFindAddressCommand;
  21.  
  22.     public void startApp() {
  23.         init();
  24.         canvas.setTitle("IP Search");
  25.  
  26.         mapFindAddressCommand=new Command("Find Address"){
  27.             public void actionPerformed(ActionEvent evt) {
  28.                  map.getIpLocations("58.192.32.1");
  29.  
  30.             }
  31.         };
  32.         canvas.addCommand(mapFindAddressCommand);
  33.         GeoLatLng center =  new GeoLatLng(32.0616667, 118.7777778);
  34.         map.setCenter(center, 13, MapType.MICROSOFTCHINA);
  35.         map.setIpAddressGeocodingListener(this);
  36.  
  37.         canvas.show();
  38.     }
  39.  
  40.     public void done(String query, IpAddressLocation result) {
  41.         if (result != null && result.error.length() == 0
  42.                 && result.longitude.length() > 0
  43.                 && result.latitude.length() > 0) {
  44.             try {
  45.  
  46.                 MapPoint mapPoint = new MapPoint();
  47.                 String latLng = "[" + result.longitude + ","
  48.                         + result.latitude + ",0]";
  49.                 mapPoint.point = DigitalMap.fromStringToLatLng(latLng);
  50.                 mapPoint.setName(result.organization);
  51.                 mapPoint.setNote(result.city + " " + result.country);
  52.                 map.panTo(mapPoint.point);
  53.             } catch (Exception e) {
  54.  
  55.                 result.error = "IP_NOT_FOUND";
  56.             }
  57.         }
  58.     }
  59. }

注:目前IP查询结果总是以英文返回,如上述结果详细内容。
ISP:”China Education and Research Network”
Organization: “Nan Jing University”
Country: “CN”
City: “Nanjing”
若想知道该经纬度对应的地名,可以使用地址反编码服务。
你可以输入 127.0.0.1 查询本机地址。