百度地图实现最简单的路径规划

牛智志
2023-12-01

/*设置目的地开始导航/
if (BaiduNaviManager.isNaviInited()) {
routeplanToNavi();
}

============================
/**
* 路径规划相关
*/
private void initRoutePlan() {
RoutePlanSearch searh = RoutePlanSearch.newInstance();
PlanNode s = PlanNode.withLocation(latLng);
PlanNode e = PlanNode.withLocation(densitylatLng);
searh.drivingSearch(new DrivingRoutePlanOption().from(s).to(e));
searh.setOnGetRoutePlanResultListener(new OnGetRoutePlanResultListener() {
@Override
public void onGetWalkingRouteResult(WalkingRouteResult result) {
}

        @Override
        public void onGetTransitRouteResult(TransitRouteResult transitRouteResult) {

        }

        @Override
        public void onGetMassTransitRouteResult(MassTransitRouteResult massTransitRouteResult) {

        }

        @Override
        public void onGetDrivingRouteResult(DrivingRouteResult result) {
            if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
                Toast.makeText(PanYuanListActivity.this, "抱歉,未找到结果", Toast.LENGTH_SHORT).show();
            }
            if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
                // 起终点或途经点地址有岐义,通过以下接口获取建议查询信息
                return;
            }
            if (result.error == SearchResult.ERRORNO.NO_ERROR) {
                if (result.getRouteLines().size() > 0) {
                    DrivingRouteLine route = result.getRouteLines().get(0);
                    int distance = route.getDistance();
                    ToastUtil.show("距离" + distance + "m");

                    //可自定义后面的DrivingRouteOverlay以改变起点终点图标,路径颜色,和点击事件等
                    DrivingRouteOverlay overlay = new DrivingRouteOverlay(mBaiduMap);
                    ///mBaiduMap.setOnMarkerClickListener(overlay);
                    overlay.setData(route);
                    overlay.addToMap();
                    overlay.zoomToSpan();
                } else {
                    Log.d("route result", "结果数<0");
                    toast("暂未找到路线");
                    return;
                }
            }
        }

        @Override
        public void onGetIndoorRouteResult(IndoorRouteResult indoorRouteResult) {
        }

        @Override
        public void onGetBikingRouteResult(BikingRouteResult bikingRouteResult) {
        }
    });
}
 类似资料: