BaiduMap SDK3.1.1 — 路径规划(RoutePlan)与覆盖物(Overlay)


     百度地图SDK自从3.0.0以后就不再兼容以前版本的方法了,然而之前项目里一直采用的是2.3.5的,无意中发现这个版本无法在4.4以上Android系统手机上使用,然后研究了百度地图开发者平台中的更新日志,在SDK2.4.1上修复了这个问题,所以无奈只得更换项目所引用的jar包,还好版本的更新引起的api变动很小,那么问题来了,如果百度地图在3.0以后再修复了什么系统不兼容的bug的话,那开发者所使用的老版本sdk就坑爹了,肯定要更改很多代码,因为很多方法已经变了,要么废弃了,要么进化了,所以与其等到那时再临时抱佛脚,不如现在就深入了学习下新版的百度地图,顺便写个demo供以后借用

      先上两张简单效果图

     BaiduMap SDK3.1.1 — 路径规划(RoutePlan)与覆盖物(Overlay)_第1张图片    

给地图添加覆盖物并获取对应索引:

mMapView = (MapView) findViewById(R.id.bmapView2);
mBaiduMap = mMapView.getMap();
// 普通地图
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);

initOverlay();
mBaiduMap.setOnMarkerClickListener(new OnMarkerClickListener() {

			@Override
			public boolean onMarkerClick(Marker marker) {
				// TODO Auto-generated method stub
				final int pos = marker.getZIndex();//获取覆盖物索引

				View popview = LayoutInflater.from(MapOverlayActivity.this)
						.inflate(R.layout.map_overlay_pop, null, false);
				TextView text = (TextView) popview
						.findViewById(R.id.map_overlay_name);
				text.setText("海贼王" + pos);
				OnInfoWindowClickListener listener = null;
				listener = new OnInfoWindowClickListener() {
					public void onInfoWindowClick() {

						Toast.makeText(MapOverlayActivity.this, "luffy" + pos,
								Toast.LENGTH_SHORT).show();
						mBaiduMap.hideInfoWindow();
					}
				};
				LatLng ll = marker.getPosition();
				mInfoWindow = new InfoWindow(BitmapDescriptorFactory
						.fromView(popview), ll, -47, listener);
				mBaiduMap.showInfoWindow(mInfoWindow);
				return true;

			}
		});

/**
  * 创建五个地图覆盖物
  */
	public void initOverlay() {

		list.add(new LatLng(studioLat, studioLon));
		list.add(new LatLng(31.360673, 120.466979));
		list.add(new LatLng(31.422323, 120.607834));
		list.add(new LatLng(31.308855, 120.637154));
		list.add(new LatLng(31.395695, 120.945884));

		// add marker overlay
		for (int i = 0; i < list.size(); i++) {
			OverlayOptions overlay = new MarkerOptions().position(list.get(i))
					.icon(bd).zIndex(i).draggable(false);
			markerList.add((Marker) (mBaiduMap.addOverlay(overlay)));
		}

		MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(list.get(0));
		mBaiduMap.setMapStatus(u);

	}


接下来是路径规划的监听关键代码:

mPoiSearch = PoiSearch.newInstance();
mPoiSearch.setOnGetPoiSearchResultListener(this);
MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(17.0f);// 设置默认缩放等级
mBaiduMap.setMapStatus(msu);
mSearch = RoutePlanSearch.newInstance();

OnGetRoutePlanResultListener listener = new OnGetRoutePlanResultListener() {
			public void onGetWalkingRouteResult(WalkingRouteResult result) {
				// 获取步行线路规划结果
				if (result == null
						|| result.error != SearchResult.ERRORNO.NO_ERROR) {
					Toast.makeText(MapGuideActivity.this, "抱歉,未找到结果",
							Toast.LENGTH_SHORT).show();
				}
				if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
					// 起终点或途经点地址有岐义,通过以下接口获取建议查询信息
					// result.getSuggestAddrInfo()
					return;
				}
				if (result.error == SearchResult.ERRORNO.NO_ERROR) {
					// route = result.getRouteLines().get(0);
					WalkingRouteOverlay overlay = new WalkingRouteOverlay(
							mBaiduMap);
					mBaiduMap.setOnMarkerClickListener(overlay);
					// routeOverlay = overlay;
					overlay.setData(result.getRouteLines().get(0));
					overlay.addToMap();
					overlay.zoomToSpan();
				}
			}

			public void onGetTransitRouteResult(TransitRouteResult result) {
				// 获取公交换乘路径规划结果
				if (result == null
						|| result.error != SearchResult.ERRORNO.NO_ERROR) {
					Toast.makeText(MapGuideActivity.this, "抱歉,未找到结果",
							Toast.LENGTH_SHORT).show();
				}
				if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
					// 起终点或途经点地址有岐义,通过以下接口获取建议查询信息
					// result.getSuggestAddrInfo()
					return;
				}
				if (result.error == SearchResult.ERRORNO.NO_ERROR) {
					TransitRouteOverlay overlay = new TransitRouteOverlay(
							mBaiduMap);
					mBaiduMap.setOnMarkerClickListener(overlay);
					overlay.setData(result.getRouteLines().get(0));
					overlay.addToMap();
					overlay.zoomToSpan();
				}
			}

			public void onGetDrivingRouteResult(DrivingRouteResult result) {
				// 获取驾车线路规划结果
				if (result == null
						|| result.error != SearchResult.ERRORNO.NO_ERROR) {
					Toast.makeText(MapGuideActivity.this, "抱歉,未找到结果",
							Toast.LENGTH_SHORT).show();
				}
				if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
					// 起终点或途经点地址有岐义,通过以下接口获取建议查询信息
					// result.getSuggestAddrInfo()
					return;
				}
				if (result.error == SearchResult.ERRORNO.NO_ERROR) {
					// route = result.getRouteLines().get(0);
					DrivingRouteOverlay overlay = new DrivingRouteOverlay(
							mBaiduMap);
					// routeOverlay = overlay;
					mBaiduMap.setOnMarkerClickListener(overlay);
					overlay.setData(result.getRouteLines().get(0));
					overlay.addToMap();
					overlay.zoomToSpan();
				}
			}
		};
		mSearch.setOnGetRoutePlanResultListener(listener);




根据双方经纬度坐标获取三种路径规划方案:驾车、步行、公交
PlanNode stNode = PlanNode.withLocation(new LatLng(currentLat,
				currentLon));
PlanNode enNode = PlanNode
				.withLocation(new LatLng(studioLat, studioLon));

mSearch.drivingSearch((new DrivingRoutePlanOption()).from(stNode)
					.to(enNode));
mSearch.transitSearch((new TransitRoutePlanOption()).from(stNode)
					.to(enNode));
mSearch.walkingSearch((new WalkingRoutePlanOption()).from(stNode)
					.to(enNode));


    

  

   

你可能感兴趣的:(百度地图,路径规划,地图覆盖物,地图SDK,LatLng)