Android学习笔记之百度地图(驾车路线搜索及RouteOverlay步行路线搜索及RouteOverlay)

步行路线搜索及RouteOverlay

方式与驾车路线搜索类似,只需将mMKSearch.drivingSearch(null, start, null, end)修改为mMKSearch.walkingSearch(null, start, null, end),实现的方法改为onGetWalkingRouteResult即可,不再赘述。

驾车路线搜索及RouteOverlay

重要代码:

[java]  view plain copy
  1. MKPlanNode start = new MKPlanNode();  
  2.         // 起点:天安门  
  3.         start.pt = new GeoPoint((int) (40.003834809598516 * 1E6),  
  4.                 (int) (116.3263213634491 * 1E6));  
  5.          // 设置地图的中心   
  6.         mapController.setCenter(start.pt);  
  7.         MKPlanNode end = new MKPlanNode();  
  8.         // 终点:鸟巢  
  9.         end.pt = new GeoPoint((int)(39.99142 * 1E6),(int)(116.39026999999998* 1E6));  
  10.         // 设置驾车路线搜索策略,时间优先、费用最少或距离最短  
  11.         /* 
  12.          * ECAR_DIS_FIRST 
  13.          * public static final int ECAR_DIS_FIRST 
  14.          * 驾乘检索策略常量:最短距离 
  15.          * ECAR_FEE_FIRST 
  16.          * public static final int ECAR_FEE_FIRST 
  17.          * 驾乘检索策略常量:较少费用 
  18.          */  
  19.         //设置驾车路线规划策略.  
  20.         mKSearch.setDrivingPolicy(MKSearch.ECAR_TIME_FIRST);  
  21.         //驾乘路线搜索.  
  22.         mKSearch.drivingSearch("北京", start, "北京", end);  


实现MySearchListener的onGetTransitRouteResult(MKTransitRouteResult, int),并展示检索结果:

[java]  view plain copy
  1. public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1)  
  2.         {  
  3.             /* 
  4.              * 返回驾乘路线搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回 
  5.              */  
  6.             if (arg0 == null)  
  7.             {  
  8.                 return;  
  9.             }  
  10.             RouteOverlay routeOverlay = new RouteOverlay(BaiduMapActivity.this,  
  11.                     mapView);  
  12.             // 此处仅展示一个方案作为示例  
  13.             routeOverlay.setData(arg0.getPlan(0).getRoute(0));  
  14.             mapView.getOverlays().add(routeOverlay);  
  15.         }  


API:

drivingSearch

public int drivingSearch(java.lang.String startCity, MKPlanNode start, java.lang.String endCity, MKPlanNode end)

驾乘路线搜索.

异步函数,返回结果在MKSearchListener里的onGetDrivingRouteResult方法通知
参数:
startCity - 起点所在城市,起点为坐标时可不填
start - 搜索的起点,可以为坐标,名称任一种
endCity - 终点所在城市,终点为坐标可不填
end - 搜索的终点,可以为坐标,名称任一种
返回:
成功返回0,否则返回-1

setDrivingPolicy

public int setDrivingPolicy(int policy)

设置驾车路线规划策略. 参数为策略常量。对下次搜索有效
参数:
policy - ECAR_TIME_FIRST:时间优先;ECAR_DIS_FIRST:距离最短;ECAR_FEE_FIRST:费用最少
返回:
成功返回0,否则返回-1


具体实现:

注意:在模拟器上模拟不能显示乘车线路,不知道是我的问题,还是模拟器的问题。但在真机上能体现出路线。

[java]  view plain copy
  1. package xiaosi.baiduMap;  
  2.   
  3. import android.app.AlertDialog;  
  4. import android.content.DialogInterface;  
  5. import android.os.Bundle;  
  6.   
  7. import com.baidu.mapapi.BMapManager;  
  8. import com.baidu.mapapi.GeoPoint;  
  9. import com.baidu.mapapi.MKAddrInfo;  
  10. import com.baidu.mapapi.MKDrivingRouteResult;  
  11. import com.baidu.mapapi.MKPlanNode;  
  12. import com.baidu.mapapi.MKPoiInfo;  
  13. import com.baidu.mapapi.MKPoiResult;  
  14. import com.baidu.mapapi.MKSearch;  
  15. import com.baidu.mapapi.MKSearchListener;  
  16. import com.baidu.mapapi.MKTransitRouteResult;  
  17. import com.baidu.mapapi.MKWalkingRouteResult;  
  18. import com.baidu.mapapi.MapActivity;  
  19. import com.baidu.mapapi.MapController;  
  20. import com.baidu.mapapi.MapView;  
  21. import com.baidu.mapapi.PoiOverlay;  
  22. import com.baidu.mapapi.RouteOverlay;  
  23.   
  24. public class BaiduMapActivity extends MapActivity  
  25. {  
  26.     /** Called when the activity is first created. */  
  27.     private BMapManager mapManager = null;  
  28.     private String key = "1B79478DA01F7800AEA8602517A6D89B38151105";  
  29.     private MapView mapView = null;  
  30.     private MKSearch mKSearch;  
  31.     private MapController mapController = null;  
  32.   
  33.     @Override  
  34.     public void onCreate(Bundle savedInstanceState)  
  35.     {  
  36.         super.onCreate(savedInstanceState);  
  37.         setContentView(R.layout.main);  
  38.         mapManager = new BMapManager(getApplication());  
  39.         mapManager.init(key, null);  
  40.         super.initMapActivity(mapManager);  
  41.         mapView = (MapView) findViewById(R.id.mapView);  
  42.         // 设置启用内置的缩放控件  
  43.         mapView.setBuiltInZoomControls(true);  
  44.         // 得到mMapView的控制权,可以用它控制和驱动平移和缩放  
  45.         mapController = mapView.getController();  
  46.         // 设置地图zoom级别  
  47.         mapController.setZoom(12);  
  48.         mKSearch = new MKSearch();  
  49.         // 注意,MKSearchListener只支持一个,以最后一次设置为准  
  50.         mKSearch.init(mapManager, new MySearchListener());  
  51.           
  52.         MKPlanNode start = new MKPlanNode();  
  53.         // 起点:天安门  
  54.         start.pt = new GeoPoint((int) (40.003834809598516 * 1E6),  
  55.                 (int) (116.3263213634491 * 1E6));  
  56.          // 设置地图的中心   
  57.         mapController.setCenter(start.pt);  
  58.         MKPlanNode end = new MKPlanNode();  
  59.         // 终点:鸟巢  
  60.         end.pt = new GeoPoint((int)(39.99142 * 1E6),(int)(116.39026999999998* 1E6));  
  61.         // 设置驾车路线搜索策略,时间优先、费用最少或距离最短  
  62.         /* 
  63.          * ECAR_DIS_FIRST 
  64.          * public static final int ECAR_DIS_FIRST 
  65.          * 驾乘检索策略常量:最短距离 
  66.          * ECAR_FEE_FIRST 
  67.          * public static final int ECAR_FEE_FIRST 
  68.          * 驾乘检索策略常量:较少费用 
  69.          */  
  70.         //设置驾车路线规划策略.  
  71.         mKSearch.setDrivingPolicy(MKSearch.ECAR_TIME_FIRST);  
  72.         //驾乘路线搜索.  
  73.         mKSearch.drivingSearch("北京", start, "北京", end);  
  74.     }  
  75.   
  76.     public class MySearchListener implements MKSearchListener  
  77.     {  
  78.         public void onGetAddrResult(MKAddrInfo arg0, int arg1)  
  79.         {  
  80.             /* 
  81.              * 返回地址信息搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示结果正确,result中有相关结果信息;100表示结果正确,无相关地址信息 
  82.              */  
  83.         }  
  84.   
  85.         public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1)  
  86.         {  
  87.             /* 
  88.              * 返回驾乘路线搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回 
  89.              */  
  90.             if (arg0 == null)  
  91.             {  
  92.                 return;  
  93.             }  
  94.             RouteOverlay routeOverlay = new RouteOverlay(BaiduMapActivity.this,  
  95.                     mapView);  
  96.             // 此处仅展示一个方案作为示例  
  97.             routeOverlay.setData(arg0.getPlan(0).getRoute(0));  
  98.             mapView.getOverlays().add(routeOverlay);  
  99.         }  
  100.   
  101.         public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2)  
  102.         {  
  103.             String result = "";  
  104.             /* 
  105.              * 返回poi搜索结果。 参数: arg0 - 搜索结果 arg1 - 返回结果类型: MKSearch.TYPE_POI_LIST MKSearch.TYPE_AREA_POI_LIST MKSearch.TYPE_CITY_LIST arg2 - 错误号,0表示正确返回 
  106.              */  
  107.             if (arg0 == null)  
  108.             {  
  109.                 return;  
  110.             }  
  111.             // 清除地图上已有的所有覆盖物  
  112.             // mapView.getOverlays().clear();  
  113.             // PoiOverlay是baidu map api提供的用于显示POI的Overlay  
  114.             PoiOverlay poioverlay = new PoiOverlay(BaiduMapActivity.this,  
  115.                     mapView);  
  116.             // 在地图上显示PoiOverlay(将搜索到的兴趣点标注在地图上)  
  117.             poioverlay.setData(arg0.getAllPoi());  
  118.             // 为地图添加覆盖物  
  119.             mapView.getOverlays().add(poioverlay);  
  120.             // 刚开始忘记加这几句代码,地图一直没改变,纠结了很长时间  
  121.             if (arg0.getNumPois() > 0)  
  122.             {  
  123.                 // 设置其中一个搜索结果所在地理坐标为地图的中心  
  124.                 MKPoiInfo poiInfo = arg0.getPoi(0);  
  125.                 mapController.setCenter(poiInfo.pt);  
  126.             }  
  127.             // 遍历当前页返回的搜索结果(默认只返回10个)  
  128.             for (MKPoiInfo poiInfo : arg0.getAllPoi())  
  129.             {  
  130.                 result = result + "\n" + "名称:" + poiInfo.name + "\n" + "地址:"  
  131.                         + poiInfo.address + "\n" + "城市:" + poiInfo.city;  
  132.             }  
  133.             // 用AlertDialog来显示搜索到的内容  
  134.             AlertDialog.Builder builder = new AlertDialog.Builder(  
  135.                     BaiduMapActivity.this);  
  136.             builder.setTitle("搜索结果");  
  137.             builder.setMessage(result);  
  138.             builder.setPositiveButton("关闭",  
  139.                     new android.content.DialogInterface.OnClickListener() {  
  140.                         public void onClick(DialogInterface dialog, int which)  
  141.                         {  
  142.                             dialog.dismiss();  
  143.                         }  
  144.                     });  
  145.             builder.show();  
  146.         }  
  147.   
  148.         public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1)  
  149.         {  
  150.             /* 
  151.              * 返回公交搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回, 当返回MKEvent.ERROR_ROUTE_ADDR时,表示起点或终点有歧义, 调用MKTransitRouteResult的getAddrResult方法获取推荐的起点或终点信息 
  152.              */  
  153.         }  
  154.   
  155.         public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1)  
  156.         {  
  157.             /* 
  158.              * 返回步行路线搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回 
  159.              */  
  160.         }  
  161.     }  
  162.   
  163.     @Override  
  164.     protected boolean isRouteDisplayed()  
  165.     {  
  166.         return false;  
  167.     }  
  168.   
  169.     @Override  
  170.     protected void onDestroy()  
  171.     {  
  172.         if (mapManager != null)  
  173.         {  
  174.             mapManager.destroy();  
  175.             mapManager = null;  
  176.         }  
  177.         super.onDestroy();  
  178.     }  
  179.   
  180.     @Override  
  181.     protected void onPause()  
  182.     {  
  183.         if (mapManager != null)  
  184.         {  
  185.             mapManager.stop();  
  186.         }  
  187.         super.onPause();  
  188.     }  
  189.   
  190.     @Override  
  191.     protected void onResume()  
  192.     {  
  193.         if (mapManager != null)  
  194.         {  
  195.             mapManager.start();  
  196.         }  
  197.         super.onResume();  
  198.     }  
  199. }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值