百度地圖路線規劃路線的起始圖示的點選監聽
阿新 • • 發佈:2019-01-27
最近專案中用到百度地圖,其中一個功能是點選路徑規劃生成的路線的終點圖示彈出InfoWindow,顯示目的地資訊。
我是這樣解決的:
在DrivingRouteOverlay中,
@Override public final List<OverlayOptions> getOverlayOptions(){
在此方法中新增路線節點、起始點圖示的對應資訊,等到需要點選監聽時,根據根據不同的對應資訊,做相應處理。
}
具體程式碼:
@Override public final List<OverlayOptions> getOverlayOptions() { if (mRouteLine == null) { return null; } List<OverlayOptions> overlayOptionses = new ArrayList<OverlayOptions>(); // step node if (mRouteLine.getAllStep() != null && mRouteLine.getAllStep().size() > 0) { for (DrivingStep step : mRouteLine.getAllStep()) { Bundle b = new Bundle(); b.putInt("index", mRouteLine.getAllStep().indexOf(step)); b.putString("end","jiedian"); if (step.getEntrance() != null) { overlayOptionses.add((new MarkerOptions()) .position(step.getEntrance().getLocation()) .anchor(0.5f, 0.5f) .zIndex(10) .rotate((360 - step.getDirection())) .extraInfo(b) .icon(BitmapDescriptorFactory .fromAssetWithDpi("Icon_line_node.png"))); //Icon_line_node.png } // 最後路段繪製出口點 if (mRouteLine.getAllStep().indexOf(step) == (mRouteLine .getAllStep().size() - 1) && step.getExit() != null) { Bundle exit = new Bundle(); exit.putString("end","exit"); overlayOptionses.add((new MarkerOptions()) .position(step.getExit().getLocation()) .anchor(0.5f, 0.5f) .extraInfo(exit) .zIndex(10) .icon(BitmapDescriptorFactory .fromAssetWithDpi("Icon_line_node.png"))); //Icon_line_node.png } } } if (mRouteLine.getStarting() != null) { Bundle start= new Bundle(); start.putString("end","start"); overlayOptionses.add((new MarkerOptions()) .position(mRouteLine.getStarting().getLocation()) .icon(getStartMarker() != null ? getStartMarker() : BitmapDescriptorFactory .fromAssetWithDpi("Icon_start.png")) .extraInfo(start) .zIndex(10)); //Icon_start.png } if (mRouteLine.getTerminal() != null) { Bundle end = new Bundle(); end.putString("end","end"); overlayOptionses .add((new MarkerOptions()) .position(mRouteLine.getTerminal().getLocation()) .icon(getTerminalMarker() != null ? getTerminalMarker() : BitmapDescriptorFactory .fromAssetWithDpi("Icon_end.png")) //Icon_end.png .extraInfo(end) .zIndex(10)); } // poly line if (mRouteLine.getAllStep() != null && mRouteLine.getAllStep().size() > 0) { List<DrivingStep> steps = mRouteLine.getAllStep(); int stepNum = steps.size(); List<LatLng> points = new ArrayList<LatLng>(); ArrayList<Integer> traffics = new ArrayList<Integer>(); int totalTraffic = 0; for (int i = 0; i < stepNum ; i++) { if (i == stepNum - 1) { points.addAll(steps.get(i).getWayPoints()); } else { points.addAll(steps.get(i).getWayPoints().subList(0, steps.get(i).getWayPoints().size() - 1)); } totalTraffic += steps.get(i).getWayPoints().size() - 1; if (steps.get(i).getTrafficList() != null && steps.get(i).getTrafficList().length > 0) { for (int j = 0;j < steps.get(i).getTrafficList().length;j++) { traffics.add(steps.get(i).getTrafficList()[j]); } } } // Bundle indexList = new Bundle(); // if (traffics.size() > 0) { // int raffic[] = new int[traffics.size()]; // int index = 0; // for (Integer tempTraff : traffics) { // raffic[index] = tempTraff.intValue(); // index++; // } // indexList.putIntArray("indexs", raffic); // } boolean isDotLine = false; if (traffics != null && traffics.size() > 0) { isDotLine = true; } PolylineOptions option = new PolylineOptions().points(points).textureIndex(traffics) .width(7).dottedLine(isDotLine).focus(true) .color(getLineColor() != 0 ? getLineColor() : Color.argb(178, 0, 78, 255)).zIndex(0); if (isDotLine) { option.customTextureList(getCustomTextureList()); } overlayOptionses.add(option); } return overlayOptionses; }
在MyOverlay中,
@Override public boolean onMarkerClick(Marker marker) { for (Overlay mMarker : mOverlayList) { if (mMarker instanceof Marker && mMarker.equals(marker)) { if (marker.getExtraInfo() != null&&marker.getExtraInfo().getString("end").equals("end")) { //點選終點圖示後做相應的操作. mBaiduMap.showInfoWindow(mudiInfoWindow); //顯示氣泡 }else{ } } } return true; }