Android學習筆記之百度地圖(駕車路線搜尋及RouteOverlay步行路線搜尋及RouteOverlay)
阿新 • • 發佈:2018-11-19
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
步行路線搜尋及RouteOverlay
方式與駕車路線搜尋類似,只需將mMKSearch.drivingSearch(null, start, null, end)修改為mMKSearch.walkingSearch(null, start, null, end),實現的方法改為onGetWalkingRouteResult即可,不再贅述。
重要程式碼:
MKPlanNode start = new MKPlanNode(); // 起點:天安門 start.pt = new GeoPoint((int) (40.003834809598516 * 1E6), (int) (116.3263213634491 * 1E6)); // 設定地圖的中心 mapController.setCenter(start.pt); MKPlanNode end = new MKPlanNode(); // 終點:鳥巢 end.pt = new GeoPoint((int)(39.99142 * 1E6),(int)(116.39026999999998* 1E6)); // 設定駕車路線搜尋策略,時間優先、費用最少或距離最短 /* * ECAR_DIS_FIRST * public static final int ECAR_DIS_FIRST * 駕乘檢索策略常量:最短距離 * ECAR_FEE_FIRST * public static final int ECAR_FEE_FIRST * 駕乘檢索策略常量:較少費用 */ //設定駕車路線規劃策略. mKSearch.setDrivingPolicy(MKSearch.ECAR_TIME_FIRST); //駕乘路線搜尋. mKSearch.drivingSearch("北京", start, "北京", end);
實現MySearchListener的onGetTransitRouteResult(MKTransitRouteResult, int),並展示檢索結果:
public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) { /* * 返回駕乘路線搜尋結果。 引數: arg0 - 搜尋結果 arg1 - 錯誤號,0表示正確返回 */ if (arg0 == null) { return; } RouteOverlay routeOverlay = new RouteOverlay(BaiduMapActivity.this, mapView); // 此處僅展示一個方案作為示例 routeOverlay.setData(arg0.getPlan(0).getRoute(0)); mapView.getOverlays().add(routeOverlay); }
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
具體實現:
注意:在模擬器上模擬不能顯示乘車線路,不知道是我的問題,還是模擬器的問題。但在真機上能體現出路線。
package xiaosi.baiduMap;import android.app.AlertDialog;import android.content.DialogInterface;import android.os.Bundle;import com.baidu.mapapi.BMapManager;import com.baidu.mapapi.GeoPoint;import com.baidu.mapapi.MKAddrInfo;import com.baidu.mapapi.MKDrivingRouteResult;import com.baidu.mapapi.MKPlanNode;import com.baidu.mapapi.MKPoiInfo;import com.baidu.mapapi.MKPoiResult;import com.baidu.mapapi.MKSearch;import com.baidu.mapapi.MKSearchListener;import com.baidu.mapapi.MKTransitRouteResult;import com.baidu.mapapi.MKWalkingRouteResult;import com.baidu.mapapi.MapActivity;import com.baidu.mapapi.MapController;import com.baidu.mapapi.MapView;import com.baidu.mapapi.PoiOverlay;import com.baidu.mapapi.RouteOverlay;public class BaiduMapActivity extends MapActivity{ /** Called when the activity is first created. */ private BMapManager mapManager = null; private String key = "1B79478DA01F7800AEA8602517A6D89B38151105"; private MapView mapView = null; private MKSearch mKSearch; private MapController mapController = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mapManager = new BMapManager(getApplication()); mapManager.init(key, null); super.initMapActivity(mapManager); mapView = (MapView) findViewById(R.id.mapView); // 設定啟用內建的縮放控制元件 mapView.setBuiltInZoomControls(true); // 得到mMapView的控制權,可以用它控制和驅動平移和縮放 mapController = mapView.getController(); // 設定地圖zoom級別 mapController.setZoom(12); mKSearch = new MKSearch(); // 注意,MKSearchListener只支援一個,以最後一次設定為準 mKSearch.init(mapManager, new MySearchListener()); MKPlanNode start = new MKPlanNode(); // 起點:天安門 start.pt = new GeoPoint((int) (40.003834809598516 * 1E6), (int) (116.3263213634491 * 1E6)); // 設定地圖的中心 mapController.setCenter(start.pt); MKPlanNode end = new MKPlanNode(); // 終點:鳥巢 end.pt = new GeoPoint((int)(39.99142 * 1E6),(int)(116.39026999999998* 1E6)); // 設定駕車路線搜尋策略,時間優先、費用最少或距離最短 /* * ECAR_DIS_FIRST * public static final int ECAR_DIS_FIRST * 駕乘檢索策略常量:最短距離 * ECAR_FEE_FIRST * public static final int ECAR_FEE_FIRST * 駕乘檢索策略常量:較少費用 */ //設定駕車路線規劃策略. mKSearch.setDrivingPolicy(MKSearch.ECAR_TIME_FIRST); //駕乘路線搜尋. mKSearch.drivingSearch("北京", start, "北京", end); } public class MySearchListener implements MKSearchListener { public void onGetAddrResult(MKAddrInfo arg0, int arg1) { /* * 返回地址資訊搜尋結果。 引數: arg0 - 搜尋結果 arg1 - 錯誤號,0表示結果正確,result中有相關結果資訊;100表示結果正確,無相關地址資訊 */ } public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) { /* * 返回駕乘路線搜尋結果。 引數: arg0 - 搜尋結果 arg1 - 錯誤號,0表示正確返回 */ if (arg0 == null) { return; } RouteOverlay routeOverlay = new RouteOverlay(BaiduMapActivity.this, mapView); // 此處僅展示一個方案作為示例 routeOverlay.setData(arg0.getPlan(0).getRoute(0)); mapView.getOverlays().add(routeOverlay); } public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) { String result = ""; /* * 返回poi搜尋結果。 引數: arg0 - 搜尋結果 arg1 - 返回結果型別: MKSearch.TYPE_POI_LIST MKSearch.TYPE_AREA_POI_LIST MKSearch.TYPE_CITY_LIST arg2 - 錯誤號,0表示正確返回 */ if (arg0 == null) { return; } // 清除地圖上已有的所有覆蓋物 // mapView.getOverlays().clear(); // PoiOverlay是baidu map api提供的用於顯示POI的Overlay PoiOverlay poioverlay = new PoiOverlay(BaiduMapActivity.this, mapView); // 在地圖上顯示PoiOverlay(將搜尋到的興趣點標註在地圖上) poioverlay.setData(arg0.getAllPoi()); // 為地圖新增覆蓋物 mapView.getOverlays().add(poioverlay); // 剛開始忘記加這幾句程式碼,地圖一直沒改變,糾結了很長時間 if (arg0.getNumPois() > 0) { // 設定其中一個搜尋結果所在地理座標為地圖的中心 MKPoiInfo poiInfo = arg0.getPoi(0); mapController.setCenter(poiInfo.pt); } // 遍歷當前頁返回的搜尋結果(預設只返回10個) for (MKPoiInfo poiInfo : arg0.getAllPoi()) { result = result + "\n" + "名稱:" + poiInfo.name + "\n" + "地址:" + poiInfo.address + "\n" + "城市:" + poiInfo.city; } // 用AlertDialog來顯示搜尋到的內容 AlertDialog.Builder builder = new AlertDialog.Builder( BaiduMapActivity.this); builder.setTitle("搜尋結果"); builder.setMessage(result); builder.setPositiveButton("關閉", new android.content.DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.show(); } public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) { /* * 返回公交搜尋結果。 引數: arg0 - 搜尋結果 arg1 - 錯誤號,0表示正確返回, 當返回MKEvent.ERROR_ROUTE_ADDR時,表示起點或終點有歧義, 呼叫MKTransitRouteResult的getAddrResult方法獲取推薦的起點或終點資訊 */ } public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) { /* * 返回步行路線搜尋結果。 引數: arg0 - 搜尋結果 arg1 - 錯誤號,0表示正確返回 */ } } @Override protected boolean isRouteDisplayed() { return false; } @Override protected void onDestroy() { if (mapManager != null) { mapManager.destroy(); mapManager = null; } super.onDestroy(); } @Override protected void onPause() { if (mapManager != null) { mapManager.stop(); } super.onPause(); } @Override protected void onResume() { if (mapManager != null) { mapManager.start(); } super.onResume(); }}