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

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

引路蜂地图API中同样提供了地址查询,路径查询,本地搜索,IP地址查询,地址反编码(通过经纬度查地名)等。 

地址查询(或称为地址编码)是将输入的地名(如南京林业大学)转换成对应的经纬度坐标然后将其显示在地图上。 

  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.MapPoint;
  15. import com.mapdigit.gis.geometry.GeoLatLng;
  16. import com.mapdigit.gis.raster.MapType;
  17. import com.mapdigit.gis.service.IGeocodingListener;
  18. import com.pstreets.gisengine.demo.MapDemoMIDP;
  19. import javax.microedition.lcdui.Command;
  20. import javax.microedition.lcdui.CommandListener;
  21. import javax.microedition.lcdui.Display;
  22. import javax.microedition.lcdui.Displayable; 
  23. //[------------------------------ MAIN CLASS ----------------------------------]
  24. //--------------------------------- REVISIONS ----------------------------------
  25. // Date       Name                 Tracking #         Description
  26. // --------   -------------------  -------------      --------------------------
  27. // 28JAN2011  James Shen                              Initial Creation
  28. ////////////////////////////////////////////////////////////////////////////////
  29. /**
  30.  *  map pan demo for Guidebee Map API on MIDP platform.
  31.  * <hr><b>&copy; Copyright 2011 Guidebee, Inc. All Rights Reserved.</b>
  32.  * @version     1.00, 28/01/11
  33.  * @author      Guidebee Pty Ltd.
  34.  */
  35. public class MapGeocodingMIDP extends MapDemoMIDP implements CommandListener,
  36.         IGeocodingListener { 
  37.  
  38.     private Command mapFindAddressCommand = new Command("Find Address",
  39.             Command.OK, 1); 
  40.  
  41.     public void startApp() { 
  42.  
  43.         init();
  44.         canvas.addCommand(mapFindAddressCommand);
  45.         map.setGeocodingListener(this);
  46.         canvas.setCommandListener(this);
  47.         GeoLatLng center = new GeoLatLng(32.0616667, 118.7777778);
  48.         map.setCenter(center, 13, MapType.MICROSOFTCHINA);
  49.         Display.getDisplay(this).setCurrent(canvas);
  50.     } 
  51.  
  52.     public void commandAction(Command c, Displayable d) {
  53.         if (c == mapFindAddressCommand) {
  54.             String name = "南京林业大学";
  55.             map.getLocations(name);
  56.         }
  57.     } 
  58.  
  59.     public void done(String query, MapPoint[] result) {
  60.         if (result != null) {
  61.             map.panTo(result[0].getPoint());
  62.         }
  63.     }
  64. }

所有的地图服务都是采用异步方式调用,在调用RasterMap.getLocation(address)前,需要设置好返回结果时的回调函数RasterMap.setGeocodingListener,回调函数接口定义为IGeocodingListener。 回调方法为public void done(String query,MapPoint[] result) ,如果查询结果不为空,则reusult 为查询结果的数组。示例中将地图转到第一个查询结果。

对于MapAbc 地图服务,还可以指定城市编码,如南京编码为25。
public void getLocation(int citycode,String query, IGeocodingListener listener);