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

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

可以通过RasterMap的getDirection()方法来查询路径,和查询地址类似,路径查询的结果也是通过回调函数的方式来通知应用程序的,下面的例子返回南京到北京的路径。返回结果存放在MapDirection中,MapDirection包含了路径的详细信息,包括路径的每个步骤,长度,时间,方向等。

  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.  
  14. //--------------------------------- IMPORTS ------------------------------------
  15. import com.mapdigit.gis.MapDirection;
  16. import com.mapdigit.gis.geometry.GeoLatLng;
  17. import com.mapdigit.gis.raster.MapType;
  18. import com.mapdigit.gis.service.IRoutingListener;
  19. import com.pstreets.gisengine.demo.MapDemoMIDP;
  20. import javax.microedition.lcdui.Command;
  21. import javax.microedition.lcdui.CommandListener;
  22. import javax.microedition.lcdui.Display;
  23. import javax.microedition.lcdui.Displayable;
  24.  
  25.  
  26. //[------------------------------ MAIN CLASS ----------------------------------]
  27. //--------------------------------- REVISIONS ----------------------------------
  28. // Date       Name                 Tracking #         Description
  29. // --------   -------------------  -------------      --------------------------
  30. // 28JAN2011  James Shen                              Initial Creation
  31. ////////////////////////////////////////////////////////////////////////////////
  32. /**
  33.  *  Map routing 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 MapRoutingMIDP extends MapDemoMIDP implements CommandListener,
  39.         IRoutingListener {
  40.  
  41.  
  42.     private Command mapGetDirectionCommand = new Command("Get Direction",
  43.             Command.OK, 1);
  44.  
  45.  
  46.     public void startApp() {
  47.  
  48.  
  49.         init();
  50.         canvas.addCommand(mapGetDirectionCommand);
  51.         map.setRoutingListener(this);
  52.         canvas.setCommandListener(this);
  53.         GeoLatLng center = new GeoLatLng(-31.948275, 115.857562);
  54.         map.setCenter(center, 13, MapType.GOOGLEMAP);
  55.  
  56.  
  57.         Display.getDisplay(this).setCurrent(canvas);
  58.     }
  59.  
  60.  
  61.     public void commandAction(Command c, Displayable d) {
  62.         if (c == mapGetDirectionCommand) {
  63.             String name1 = "南京";
  64.             String name2 = "北京";
  65.             map.getDirections("from: " + name1 + " to: " + name2);
  66.  
  67.  
  68.         }
  69.     }
  70.  
  71.  
  72.     public void done(String query, MapDirection result) {
  73.         if (result != null) {
  74.             map.setMapDirection(result);
  75.             map.resize(result.getBound());
  76.             map.setZoom(3);
  77.  
  78.  
  79.         }
  80.     }
  81. }

 

   

地图服务可以选择使用Google 地图服务,CloudMade地图服务,在中国还可能选择MapAbc地图服务,缺省使用Google 地图服务。
getDirections()具有三个重载函数,例子中是采用的文字描述方式。上述示例采用了from: address1 to: address2 的格式, CloudMade地图服务和MapAbc地图服务则必需采用 经度1,纬度1,经度2,纬度2和格式。
为避免混淆,可以使用下述格式。
public void getDirection(GeoLatLng[] waypoints, IRoutingListener listener);
其中 waypoints 为途径点坐标数组经纬值,可以支持多点路径查询。
此外对于MapAbc 地图服务,还可以指定城市编码,如南京编码为25。
public void getDirection(int citycode,String query, IRoutingListener listener);