山东大学项目实训-地图圈系统-APP(5)

安卓开发地图路径规划


注意:高德地图的overlay类需要自己去引入

一、步行
RouteSearch.WalkRouteQuery query2 = new RouteSearch.WalkRouteQuery(fromAndTo);
routeSearch.calculateWalkRouteAsyn(query2);
@Override
    public void onWalkRouteSearched(WalkRouteResult result, int errorCode) {
        //步行路线规划
        aMap.clear();// 清理地图上的所有覆盖物
        if (errorCode == AMapException.CODE_AMAP_SUCCESS) {
            if (result != null && result.getPaths() != null) {
                if (result.getPaths().size() > 0) {
                    mWalkRouteResult = result;
                    final WalkPath walkPath = mWalkRouteResult.getPaths()
                            .get(0);
                    WalkRouteOverlay walkRouteOverlay = new WalkRouteOverlay(this, aMap, walkPath, mWalkRouteResult.getStartPos(), mWalkRouteResult.getTargetPos());

                    //计算所有点经纬度
                    List<WalkStep> walkPaths = walkPath.getSteps();
                    List<LatLng> pass_latlng;
                    pass_latlng=new ArrayList<LatLng>();
                    pass_latlng.add(AMapServicesUtil.convertToLatLng(mWalkRouteResult.getStartPos()));
                    for (int i = 0; i < walkPaths.size(); i++) {
                        WalkStep walkStep = walkPaths.get(i);
                        LatLng latLng = AMapServicesUtil.convertToLatLng(walkStep
                                .getPolyline().get(0));
                        pass_latlng.add(latLng);
                    }
                    pass_latlng.add(AMapServicesUtil.convertToLatLng(mWalkRouteResult.getTargetPos()));


                    walkRouteOverlay.removeFromMap();

                    //分析源码,找到addToMap()方法可以获得路径详情
                    walk_details= walkRouteOverlay.addToMap();
                    System.out.println(walk_details.toString());

                    walkRouteOverlay.zoomToSpan(pass_latlng);
                    walkRouteOverlay.setNodeIconVisibility(false);
                    int dis = (int) walkPath.getDistance();
                    long dur = (int) walkPath.getDuration();
                    int round = Math.round(dis);
                    String gl = Tools.mi_change_kil(dis);
                    String ti = Tools.get_h_m_s(dur);
                    String dur2 = AMapUtil.getFriendlyTime((int) walkPath.getDuration());
                    String dis2 = AMapUtil
                            .getFriendlyLength((int) walkPath.getDistance());
                    System.out.println("距离目的地约"+ gl+"步行预计"+ ti +"后到达");
                    route_time_distance.setText(dur2+"  "+dis2);
                    get_walk_route_details.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(mContext, WalkRouteDetailActivity.class);
                            intent.putExtra("walk_path", walkPath);
                            intent.putExtra("walk_result",
                                    mWalkRouteResult);
                            startActivity(intent);
                        }
                    });

                    mWalkRouteResult.getWalkQuery();
                } else if (result != null && result.getPaths() == null) {
//                    ToastUtil.show(mContext, R.string.no_result);
                    System.out.println("错误");
                }
            } else {
//                ToastUtil.show(mContext, R.string.no_result);
                System.out.println("错误");
            }
        } else {
//            ToastUtil.showerror(this.getApplicationContext(), errorCode);
            System.out.println("错误");
        }
    }
二、驾车
RouteSearch.DriveRouteQuery query = new RouteSearch.DriveRouteQuery(fromAndTo, DRIVING_SINGLE_DEFAULT, null, null, "");
routeSearch.calculateDriveRouteAsyn(query);
@Override
    public void onDriveRouteSearched(DriveRouteResult driveRouteResult, int errorCode) {
        aMap.clear();// 清理地图上的所有覆盖物
        if (errorCode == AMapException.CODE_AMAP_SUCCESS) {
            if (driveRouteResult != null && driveRouteResult.getPaths() != null) {
                if (driveRouteResult.getPaths().size() > 0) {
                    mDriveRouteResult = driveRouteResult;
                    final DrivePath drivePath = mDriveRouteResult.getPaths()
                            .get(0);
                    if(drivePath == null) {
                        return;
                    }
                    DrivingRouteOverlay drivingRouteOverlay = new DrivingRouteOverlay(
                            this, aMap, drivePath,
                            mDriveRouteResult.getStartPos(),
                            mDriveRouteResult.getTargetPos(), null);

                    //计算所有点经纬度
                    List<DriveStep> drivePaths = drivePath.getSteps();
                    List<LatLng> pass_latlng;
                    pass_latlng=new ArrayList<LatLng>();
                    pass_latlng.add(AMapServicesUtil.convertToLatLng(mDriveRouteResult.getStartPos()));
                    for (int i = 0; i < drivePaths.size(); i++) {
                        DriveStep driveStep = drivePaths.get(i);
                        LatLng latLng = AMapServicesUtil.convertToLatLng(driveStep
                                .getPolyline().get(0));
                        pass_latlng.add(latLng);
                    }
                    pass_latlng.add(AMapServicesUtil.convertToLatLng(mDriveRouteResult.getTargetPos()));

                    drivingRouteOverlay.setNodeIconVisibility(false);//设置节点marker是否显示
                    drivingRouteOverlay.setIsColorfulline(true);//是否用颜色展示交通拥堵情况,默认true
                    drivingRouteOverlay.removeFromMap();
                    drivingRouteOverlay.addToMap();
                    drivingRouteOverlay.zoomToSpan(pass_latlng);
//                    mBottomLayout.setVisibility(View.VISIBLE);
                    int dis = (int) drivePath.getDistance();
                    int dur = (int) drivePath.getDuration();
                    String des = AMapUtil.getFriendlyTime(dur)+"("+AMapUtil.getFriendlyLength(dis)+")";
                    route_time_distance.setText(AMapUtil.getFriendlyTime(dur)+"  "+AMapUtil.getFriendlyLength(dis));

                    get_walk_route_details.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(mContext,
                                    DriveRouteDetailActivity.class);
                            intent.putExtra("drive_path", drivePath);
                            intent.putExtra("drive_result",
                                    mDriveRouteResult);
                            startActivity(intent);
                        }
                    });

                } else if (driveRouteResult != null && driveRouteResult.getPaths() == null) {
                    System.out.println("error");
                }

            } else {
                System.out.println("error");            }
        } else {
            System.out.println("error");
        }
    }
三、骑行
RouteSearch.RideRouteQuery query3 = new RouteSearch.RideRouteQuery(fromAndTo);
routeSearch.calculateRideRouteAsyn(query3);
@Override
    public void onRideRouteSearched(RideRouteResult result, int errorCode) {
        aMap.clear();// 清理地图上的所有覆盖物
        if (errorCode == AMapException.CODE_AMAP_SUCCESS) {
            if (result != null && result.getPaths() != null) {
                if (result.getPaths().size() > 0) {
                    mRideRouteResult = result;
                    final RidePath ridePath = mRideRouteResult.getPaths()
                            .get(0);
                    if(ridePath == null) {
                        return;
                    }
                    RideRouteOverlay rideRouteOverlay = new RideRouteOverlay(
                            this, aMap, ridePath,
                            mRideRouteResult.getStartPos(),
                            mRideRouteResult.getTargetPos());

                    //计算所有点经纬度
                    List<RideStep> ridePaths = ridePath.getSteps();
                    List<LatLng> pass_latlng;
                    pass_latlng=new ArrayList<LatLng>();
                    pass_latlng.add(AMapServicesUtil.convertToLatLng(mRideRouteResult.getStartPos()));
                    for (int i = 0; i < ridePaths.size(); i++) {
                        RideStep rideStep = ridePaths.get(i);
                        LatLng latLng = AMapServicesUtil.convertToLatLng(rideStep
                                .getPolyline().get(0));
                        pass_latlng.add(latLng);
                    }
                    pass_latlng.add(AMapServicesUtil.convertToLatLng(mRideRouteResult.getTargetPos()));

                    rideRouteOverlay.removeFromMap();
                    rideRouteOverlay.addToMap();
                    rideRouteOverlay.zoomToSpan(pass_latlng);
//                    mBottomLayout.setVisibility(View.VISIBLE);
                    int dis = (int) ridePath.getDistance();
                    int dur = (int) ridePath.getDuration();
                    String des = AMapUtil.getFriendlyTime(dur)+"("+AMapUtil.getFriendlyLength(dis)+")";
                    route_time_distance.setText(AMapUtil.getFriendlyTime(dur)+"  "+AMapUtil.getFriendlyLength(dis));
                    get_walk_route_details.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(mContext,
                                    RideRouteDetailActivity.class);
                            intent.putExtra("ride_path", ridePath);
                            intent.putExtra("ride_result",
                                    mRideRouteResult);
                            startActivity(intent);
                        }
                    });

                } else if (result != null && result.getPaths() == null) {
                }
            } else {
            }
        } else {
        }
    }
四、公交
RouteSearch.BusRouteQuery query1 = new RouteSearch.BusRouteQuery(fromAndTo, RouteSearch.BusLeaseWalk, "", 1);
routeSearch.calculateBusRouteAsyn(query1);
 @Override
    public void onBusRouteSearched(BusRouteResult result, int errorCode) {
        aMap.clear();// 清理地图上的所有覆盖物
        if (errorCode == AMapException.CODE_AMAP_SUCCESS) {
            if (result != null && result.getPaths() != null) {
                if (result.getPaths().size() > 0) {
                    mBusResultList.setVisibility(View.VISIBLE);
                    mBusRouteResult = result;
                    BusResultListAdapter mBusResultListAdapter = new BusResultListAdapter(mContext, mBusRouteResult);

                    mBusResultList.setAdapter(mBusResultListAdapter);
                } else if (result != null && result.getPaths() == null) {
                    System.out.println("失败");
                }
            } else {
                System.out.println("失败");
            }
        } else {
            System.out.println("失败");
        }
    }
效果图

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值