第16行: | 第16行: | ||
List<OverlayOptions> overlayList = new ArrayList<OverlayOptions>(); | List<OverlayOptions> overlayList = new ArrayList<OverlayOptions>(); | ||
− | if (mRouteLine.getAllStep() | + | if (mRouteLine.getAllStep()!= null |
&& mRouteLine.getAllStep().size() > 0) { | && mRouteLine.getAllStep().size() > 0) { | ||
− | for (BikingRouteLine.BikingStep step | + | for (BikingRouteLine.BikingStep step: mRouteLine.getAllStep()) { |
Bundle b = new Bundle(); | Bundle b = new Bundle(); | ||
b.putInt("index", mRouteLine.getAllStep().indexOf(step)); | b.putInt("index", mRouteLine.getAllStep().indexOf(step)); | ||
− | if (step.getEntrance() | + | if (step.getEntrance()!= null) { |
overlayList.add((new MarkerOptions()).position(step.getEntrance().getLocation()) | overlayList.add((new MarkerOptions()).position(step.getEntrance().getLocation()) | ||
.rotate((360 - step.getDirection())).zIndex(10).anchor(0.5f, 0.5f) | .rotate((360 - step.getDirection())).zIndex(10).anchor(0.5f, 0.5f) | ||
第31行: | 第31行: | ||
if (mRouteLine.getAllStep().indexOf(step) == (mRouteLine | if (mRouteLine.getAllStep().indexOf(step) == (mRouteLine | ||
.getAllStep().size() - 1) | .getAllStep().size() - 1) | ||
− | && step.getExit() | + | && step.getExit()!= null) { |
overlayList.add((new MarkerOptions()).position(step.getExit().getLocation()) | overlayList.add((new MarkerOptions()).position(step.getExit().getLocation()) | ||
.anchor(0.5f, 0.5f).zIndex(10) | .anchor(0.5f, 0.5f).zIndex(10) | ||
第40行: | 第40行: | ||
} | } | ||
// starting | // starting | ||
− | if (mRouteLine.getStarting() | + | if (mRouteLine.getStarting()!= null) { |
overlayList.add((new MarkerOptions()).position(mRouteLine.getStarting().getLocation()) | overlayList.add((new MarkerOptions()).position(mRouteLine.getStarting().getLocation()) | ||
− | .icon(getStartMarker() | + | .icon(getStartMarker()!= null? getStartMarker() |
− | + | : BitmapDescriptorFactory.fromAssetWithDpi("Icon_start.png")) | |
.zIndex(10)); | .zIndex(10)); | ||
} | } | ||
// terminal | // terminal | ||
− | if (mRouteLine.getTerminal() | + | if (mRouteLine.getTerminal()!= null) { |
overlayList.add((new MarkerOptions()).position(mRouteLine.getTerminal().getLocation()) | overlayList.add((new MarkerOptions()).position(mRouteLine.getTerminal().getLocation()) | ||
− | .icon(getTerminalMarker() | + | .icon(getTerminalMarker()!= null? getTerminalMarker() |
− | + | : BitmapDescriptorFactory.fromAssetWithDpi("Icon_end.png")) | |
.zIndex(10)); | .zIndex(10)); | ||
} | } | ||
// poly line list | // poly line list | ||
− | if (mRouteLine.getAllStep() | + | if (mRouteLine.getAllStep()!= null |
&& mRouteLine.getAllStep().size() > 0) { | && mRouteLine.getAllStep().size() > 0) { | ||
LatLng lastStepLastPoint = null; | LatLng lastStepLastPoint = null; | ||
− | for (BikingRouteLine.BikingStep step | + | for (BikingRouteLine.BikingStep step: mRouteLine.getAllStep()) { |
List<LatLng> watPoints = step.getWayPoints(); | List<LatLng> watPoints = step.getWayPoints(); | ||
− | if (watPoints | + | if (watPoints!= null) { |
List<LatLng> points = new ArrayList<LatLng>(); | List<LatLng> points = new ArrayList<LatLng>(); | ||
− | if (lastStepLastPoint | + | if (lastStepLastPoint!= null) { |
points.add(lastStepLastPoint); | points.add(lastStepLastPoint); | ||
} | } | ||
第92行: | 第92行: | ||
List<OverlayOptions> overlayOptions = getOverlayOptions(); | List<OverlayOptions> overlayOptions = getOverlayOptions(); | ||
− | if (overlayOptions | + | if (overlayOptions!= null) { |
mOverlayOptionList.addAll(getOverlayOptions()); | mOverlayOptionList.addAll(getOverlayOptions()); | ||
} | } | ||
− | for (OverlayOptions option | + | for (OverlayOptions option: mOverlayOptionList) { |
mBaiduMap.addOverlay(option) | mBaiduMap.addOverlay(option) | ||
} | } | ||
第111行: | 第111行: | ||
if (mOverlayList.size() > 0) { | if (mOverlayList.size() > 0) { | ||
LatLngBounds.Builder builder = new LatLngBounds.Builder(); | LatLngBounds.Builder builder = new LatLngBounds.Builder(); | ||
− | for (Overlay overlay | + | for (Overlay overlay: mOverlayList) { |
// polyline 中的点可能太多,只按marker 缩放 | // polyline 中的点可能太多,只按marker 缩放 | ||
if (overlay instanceof Marker) { | if (overlay instanceof Marker) { | ||
第139行: | 第139行: | ||
model.instruction = step.instruction; | model.instruction = step.instruction; | ||
NSString *instruction = step.instruction; | NSString *instruction = step.instruction; | ||
− | NSDictionary *attrs = @{NSFontAttributeName | + | NSDictionary *attrs = @{NSFontAttributeName: [UIFont fontWithName:@"PingFangSC-Regular" size:16 * widthScale]}; |
CGSize size = CGSizeMake(KScreenWidth - 86 * widthScale, MAXFLOAT); | CGSize size = CGSizeMake(KScreenWidth - 86 * widthScale, MAXFLOAT); | ||
CGSize actualsize = [instruction boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size; | CGSize actualsize = [instruction boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size; |
2022年1月21日 (五) 14:39的版本
骑行路线规划
功能场景
物流快递、出行、O2O等APP中常使用骑行路线规划功能,展示小车到目的地的路线、时间、里程信息等。
Android
iOS
//mapopen-website-wiki.bj.bcebos.com/demos/AndroidVideos/SVID_20200530_210144_1.mp4
1590746640|//mapopen-website-wiki.bj.bcebos.com/demos/newqrcodes/骑行路线规划.png
扫码体验
使用产品
Android地图SDK|/index.php?title=androidsdk
>
骑行路线规划|/index.php?title=androidsdk/guide/route/bike
下载源码
//mapopen-website-wiki.bj.bcebos.com/demos/BaiduMapSDKExample.zip
核心接口
类
接口
描述
RoutePlanSearch
newInstance()
创建路线规划检索实例
RoutePlanSearch
setOnGetRoutePlanResultListener(OnGetRoutePlanResultListener listener)
设置路线规划检索监听器
RoutePlanSearch
bikingSearch(BikingRoutePlanOption option)
发起骑行路线规划检索请求
RoutePlanSearch
destory()
释放路线规划检索实例
OnGetRoutePlanResultListener
onGetBikingRouteResult(BikingRouteResult bikingRouteResult)
骑行路线规划的结果回调函数
PlanNode
withCityNameAndPlaceName(String cityName, String placeName)
路线规划起终点参数
BikingRoutePlanOption
from(PlanNode startNode)
设置路线规划起点参数
BikingRoutePlanOption
to(PlanNode endNode)
设置路线规划终点参数
BikingRoutePlanOption
ridingType(int type)
设置骑行路线规划类型
BikingRouteOverlay
setData(BikingRouteLine routeLine)
设置骑行路线规划结果数据
BikingRouteOverlay
addToMap()
添加骑行路线添加到地图上
BikingRouteOverlay
zoomToSpanPaddingBounds(int paddingLeft, int paddingTop, int paddingRight, int paddingRight)
将骑行路线调整到地图显示的最佳视野范围内
核心代码
1.骑行路线规划结果解析
JAVA
/** * 设置路线数据。 * * @param line 路线数据 */ public void setData(BikingRouteLine line) { mRouteLine = line; } @Override public final List<OverlayOptions> getOverlayOptions() { if (mRouteLine == null) { return null; } List<OverlayOptions> overlayList = new ArrayList<OverlayOptions>(); if (mRouteLine.getAllStep()!= null && mRouteLine.getAllStep().size() > 0) { for (BikingRouteLine.BikingStep step: mRouteLine.getAllStep()) { Bundle b = new Bundle(); b.putInt("index", mRouteLine.getAllStep().indexOf(step)); if (step.getEntrance()!= null) { overlayList.add((new MarkerOptions()).position(step.getEntrance().getLocation()) .rotate((360 - step.getDirection())).zIndex(10).anchor(0.5f, 0.5f) .extraInfo(b) .icon(BitmapDescriptorFactory.fromAssetWithDpi("Icon_line_node.png"))); } // 最后路段绘制出口点 if (mRouteLine.getAllStep().indexOf(step) == (mRouteLine .getAllStep().size() - 1) && step.getExit()!= null) { overlayList.add((new MarkerOptions()).position(step.getExit().getLocation()) .anchor(0.5f, 0.5f).zIndex(10) .icon(BitmapDescriptorFactory.fromAssetWithDpi("Icon_line_node.png"))); } } } // starting if (mRouteLine.getStarting()!= null) { overlayList.add((new MarkerOptions()).position(mRouteLine.getStarting().getLocation()) .icon(getStartMarker()!= null? getStartMarker() : BitmapDescriptorFactory.fromAssetWithDpi("Icon_start.png")) .zIndex(10)); } // terminal if (mRouteLine.getTerminal()!= null) { overlayList.add((new MarkerOptions()).position(mRouteLine.getTerminal().getLocation()) .icon(getTerminalMarker()!= null? getTerminalMarker() : BitmapDescriptorFactory.fromAssetWithDpi("Icon_end.png")) .zIndex(10)); } // poly line list if (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); overlayList.add(new PolylineOptions().points(points).width(18).dottedLine(true) .customTexture(getBikeRoadCustomTexture()).zIndex(0)); lastStepLastPoint = watPoints.get(watPoints.size() - 1); } } } return overlayList; } public BitmapDescriptor getBikeRoadCustomTexture() { return BitmapDescriptorFactory.fromAsset("Icon_road_blue_arrow.png"); }
复制
深色
复制成功
2.将骑行路线添加到地图上
JAVA
/** * 将骑行路线添加到地图上 */ public final void addToMap() { if (mBaiduMap == null) { return; } List<OverlayOptions> overlayOptions = getOverlayOptions(); if (overlayOptions!= null) { mOverlayOptionList.addAll(getOverlayOptions()); } for (OverlayOptions option: mOverlayOptionList) { mBaiduMap.addOverlay(option) } }
复制
深色
复制成功
3.将骑行路线调整到地图显示的最佳视野范围内
JAVA
/** * 将骑行路线调整到地图显示的最佳视野范围内 */ public void zoomToSpanPaddingBounds(int paddingLeft, int paddingTop, int paddingRight, int paddingBottom) { if (mBaiduMap == null) { return; } if (mOverlayList.size() > 0) { LatLngBounds.Builder builder = new LatLngBounds.Builder(); for (Overlay overlay: mOverlayList) { // polyline 中的点可能太多,只按marker 缩放 if (overlay instanceof Marker) { builder.include(((Marker) overlay).getPosition()); } } mBaiduMap.setMapStatus(MapStatusUpdateFactory .newLatLngBounds(builder.build(), paddingLeft, paddingTop, paddingRight, paddingBottom)); } }
复制
深色
复制成功
更多功能
骑行导航|/index.php?title=androidsdk/guide/navigation/bikenavi