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

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

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

  1. //------------------------------------------------------------------------------
  2. //                         COPYRIGHT 2011 GUIDEBEE
  3. //                           ALL RIGHTS RESERVED.
  4. //                     GUIDEBEE CONFIDENTIAL PROPRIETARY
  5. ///////////////////////////////////// REVISIONS ////////////////////////////////
  6. // Date       Name                 Tracking #         Description
  7. // ---------  -------------------  ----------         --------------------------
  8. // 28JAN2011  James Shen                              Initial Creation
  9. ////////////////////////////////////////////////////////////////////////////////
  10. //--------------------------------- PACKAGE ------------------------------------
  11. package com.pstreets.gisengine.demo.midp;
  12.  
  13. //--------------------------------- IMPORTS ------------------------------------
  14. import com.mapdigit.gis.DigitalMap;
  15. import com.mapdigit.gis.MapPoint;
  16. import com.mapdigit.gis.geometry.GeoLatLng;
  17. import com.mapdigit.gis.raster.MapType;
  18. import com.mapdigit.gis.service.IIpAddressGeocodingListener;
  19. import com.pstreets.gisengine.demo.MapDemoMIDP;
  20. import com.mapdigit.gis.service.IpAddressLocation;
  21. import javax.microedition.lcdui.Command;
  22. import javax.microedition.lcdui.CommandListener;
  23. import javax.microedition.lcdui.Display;
  24. import javax.microedition.lcdui.Displayable;
  25.  
  26. //[------------------------------ MAIN CLASS ----------------------------------]
  27. //--------------------------------- REVISIONS ----------------------------------
  28. // Date       Name                 Tracking #         Description
  29. // --------   -------------------  -------------      --------------------------
  30. // 28JAN2011  James Shen                              Initial Creation
  31. ////////////////////////////////////////////////////////////////////////////////
  32. /**
  33.  *  map pan demo for Guidebee Map API on MIDP platform.
  34.  * <hr><b>&copy; Copyright 2011 Guidebee, Inc. All Rights Reserved.</b>
  35.  * @version     1.00, 28/01/11
  36.  * @author      Guidebee Pty Ltd.
  37.  */
  38. public class MapIpSearchMIDP extends MapDemoMIDP implements CommandListener,
  39.         IIpAddressGeocodingListener {
  40.  
  41.     private Command mapFindAddressCommand = new Command("Find Address",
  42.             Command.OK, 1);
  43.  
  44.     public void startApp() {
  45.  
  46.         init();
  47.         canvas.addCommand(mapFindAddressCommand);
  48.         map.setIpAddressGeocodingListener(this);
  49.         canvas.setCommandListener(this);
  50.         GeoLatLng center = new GeoLatLng(32.0616667, 118.7777778);
  51.         map.setCenter(center, 15, MapType.MICROSOFTCHINA);
  52.         Display.getDisplay(this).setCurrent(canvas);
  53.     }
  54.  
  55.     public void commandAction(Command c, Displayable d) {
  56.         if (c == mapFindAddressCommand) {
  57.             map.getIpLocations("58.192.32.1");
  58.         }
  59.     }
  60.  
  61.     public void done(String query, IpAddressLocation result) {
  62.         if (result != null && result.error.length() == 0
  63.                 && result.longitude.length() > 0
  64.                 && result.longitude.length() > 0) {
  65.             try {
  66.  
  67.                 MapPoint mapPoint = new MapPoint();
  68.                 String latLng = "[" + result.longitude + ","
  69.                         + result.latitude + ",0]";
  70.                 mapPoint.point = DigitalMap.fromStringToLatLng(latLng);
  71.                 mapPoint.setName(result.organization);
  72.                 mapPoint.setNote(result.city + " " + result.country);
  73.                 map.panTo(mapPoint.point);
  74.             } catch (Exception e) {
  75.  
  76.                 result.error = "IP_NOT_FOUND";
  77.             }
  78.         }
  79.     }
  80. }

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