android 百度地图骑行路线颜色及宽度更改
生活随笔
收集整理的這篇文章主要介紹了
android 百度地图骑行路线颜色及宽度更改
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
RoutePlanSearch mRoutePlanSearch.bikingSearch((new BikingRoutePlanOption()).from(stNode).to(enNode)// ridingType 0 普通騎行,1 電動車騎行// 默認普通騎行.ridingType(1));
mRoutePlanSearch = RoutePlanSearch.newInstance();
mRoutePlanSearch.setOnGetRoutePlanResultListener(new OnGetRoutePlanResultListener() {@Overridepublic void onGetWalkingRouteResult(WalkingRouteResult walkingRouteResult) {Log.d("onGetWalkingRouteResult", "onGetWalkingRouteResult");}@Overridepublic void onGetTransitRouteResult(TransitRouteResult transitRouteResult) {Log.d("onGetTransitRouteResult", "onGetTransitRouteResult");}@Overridepublic void onGetMassTransitRouteResult(MassTransitRouteResult massTransitRouteResult) {Log.d("onGetMassTransitRoute", "onGetMassTransitRouteResult");}@Overridepublic void onGetDrivingRouteResult(DrivingRouteResult drivingRouteResult) {Log.d("onGetDrivingRouteResult", "onGetDrivingRouteResult");}@Overridepublic void onGetIndoorRouteResult(IndoorRouteResult indoorRouteResult) {Log.d("onGetIndoorRouteResult", "onGetIndoorRouteResult");}@Overridepublic void onGetBikingRouteResult(BikingRouteResult bikingRouteResult) {if (bikingRouteResult == null || bikingRouteResult.error != SearchResult.ERRORNO.NO_ERROR) {Toast.makeText(context, "路線規(guī)劃失敗", Toast.LENGTH_SHORT).show();}if (bikingRouteResult.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {//起終點或途經(jīng)點地址有岐義,通過以下接口獲取建議查詢信息//result.getSuggestAddrInfo()Toast.makeText(context, "路線規(guī)劃失敗", Toast.LENGTH_SHORT).show();return;}if (bikingRouteResult.error == SearchResult.ERRORNO.ST_EN_TOO_NEAR) {Toast.makeText(context, "距離太近,無需規(guī)劃騎行路線", Toast.LENGTH_SHORT).show();return;}//創(chuàng)建BikingRouteOverlay實例mBikingRouteOverlay = new BikingRouteOverlay(mBaiduMap);if (bikingRouteResult.getRouteLines() != null && bikingRouteResult.getRouteLines().size() > 0) {//獲取路徑規(guī)劃數(shù)據(jù),(以返回的第一條路線為例)BikingRouteLine mBikingRouteLine = bikingRouteResult.getRouteLines().get(0);displayDistanceAndDuration(mBikingRouteLine);//為BikingRouteOverlay實例設(shè)置數(shù)據(jù)mBikingRouteOverlay.setData(mBikingRouteLine);//在地圖上繪制BikingRouteOverlaymBikingRouteOverlay.addToMap();}}});
bikingSearch獲取騎行規(guī)劃的路線,并通過setOnGetRoutePlanResultListener進行監(jiān)聽繪制路線,通過mBikingRouteOverlay.addToMap();繪制到地圖上。
通過點擊addToMap()(ctrl+鼠標點擊)
可以看到mPolylineOptions.color、setCustomTexture和width來控制路線的顏色、形狀和寬度
最終解決方式為重寫B(tài)ikingRouteOverlay,具體代碼如下:
/** Copyright (C) 2016 Baidu, Inc. All Rights Reserved.*/ package com.chinatower.fghd.customer.home;import android.graphics.Color; import android.os.Bundle; import android.util.Log;import com.baidu.mapapi.map.BaiduMap; import com.baidu.mapapi.map.BitmapDescriptor; import com.baidu.mapapi.map.BitmapDescriptorFactory; import com.baidu.mapapi.map.Marker; import com.baidu.mapapi.map.MarkerOptions; import com.baidu.mapapi.map.Overlay; import com.baidu.mapapi.map.OverlayOptions; import com.baidu.mapapi.map.Polyline; import com.baidu.mapapi.map.PolylineOptions; import com.baidu.mapapi.model.LatLng; import com.baidu.mapapi.search.route.BikingRouteLine;import java.util.ArrayList; import java.util.List;/*** 用于顯示騎行路線的Overlay*/ public class BikingRouteOverlay extends OverlayManager {private BikingRouteLine mRouteLine = null;public BikingRouteOverlay(BaiduMap baiduMap) {super(baiduMap);}/*** 設(shè)置路線數(shù)據(jù)。** @param line 路線數(shù)據(jù)*/public void setData(BikingRouteLine line) {mRouteLine = line;}@Overridepublic final List<OverlayOptions> getOverlayOptions() {if (mRouteLine == null) {return null;}List<OverlayOptions> overlayList = new ArrayList<OverlayOptions>();if (mRouteLine.getStarting() != null) {overlayList.add((new MarkerOptions()).position(mRouteLine.getStarting().getLocation()).icon(getStartMarker() != null ? getStartMarker() :BitmapDescriptorFactory.fromAssetWithDpi("Icon_start.png")).zIndex(0).yOffset(-50));}// terminalif (mRouteLine.getTerminal() != null) {overlayList.add((new MarkerOptions()).position(mRouteLine.getTerminal().getLocation()).icon(getTerminalMarker() != null ? getTerminalMarker() :BitmapDescriptorFactory.fromAssetWithDpi("Icon_end.png")).zIndex(0).yOffset(-50));}// poly line listif (mRouteLine.getAllStep() != null&& mRouteLine.getAllStep().size() > 0) {LatLng lastStepLastPoint = null;for (BikingRouteLine.BikingStep step : mRouteLine.getAllStep()) {List<LatLng> watPoints = step.getWayPoints();if (watPoints != null) {List<LatLng> points = new ArrayList<LatLng>();if (lastStepLastPoint != null) {points.add(lastStepLastPoint);}points.addAll(watPoints);**//關(guān)鍵代碼**overlayList.add(new PolylineOptions().points(points).width(12).color(getLineColor() != 0 ? getLineColor() : Color.argb(100, 50, 69, 90)).zIndex(0));lastStepLastPoint = watPoints.get(watPoints.size() - 1);}}}return overlayList;}/*** 覆寫此方法以改變默認起點圖標** @return 起點圖標*/public BitmapDescriptor getStartMarker() {return null;}public int getLineColor() {return 0;}/*** 覆寫此方法以改變默認終點圖標** @return 終點圖標*/public BitmapDescriptor getTerminalMarker() {return null;}/*** 處理點擊事件** @param i 被點擊的step在* {@link BikingRouteLine#getAllStep()}* 中的索引* @return 是否處理了該點擊事件*/public boolean onRouteNodeClick(int i) {if (mRouteLine.getAllStep() != null&& mRouteLine.getAllStep().get(i) != null) {Log.i("baidumapsdk", "BikingRouteOverlay onRouteNodeClick");}return false;}@Overridepublic final boolean onMarkerClick(Marker marker) {for (Overlay mMarker : mOverlayList) {if (mMarker instanceof Marker && mMarker.equals(marker)) {if (marker.getExtraInfo() != null) {onRouteNodeClick(marker.getExtraInfo().getInt("index"));}}}return true;}@Overridepublic boolean onPolylineClick(Polyline polyline) {// TODO Auto-generated method stubreturn false;} }總結(jié)
以上是生活随笔為你收集整理的android 百度地图骑行路线颜色及宽度更改的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mediawiki内嵌php,Media
- 下一篇: gomarket服务器位置,V712双核