高德地图线路规划的接入使用(步行-公交-驾车)

参考文案:http://lbs.amap.com/api/android-sdk/guide/route-plan/drive/

官方文档:http://a.amap.com/lbs/static/unzip/Android_Map_Doc/index.html


 /**
     * 计算步行路线
     */
    private void calculateWalkRoute() {
        if (routeSearch == null){
            routeSearch = new RouteSearch(this);
        }
        if (latitude != -1 && longitude != -1) {
            fromAndTo = new RouteSearch.FromAndTo(new LatLonPoint(lat, lng), new LatLonPoint(latitude, longitude));
        }
        routeSearch.setRouteSearchListener(this);
        //初始化query对象,fromAndTo是包含起终点信息,walkMode是步行路径规划的模式
        RouteSearch.WalkRouteQuery walkRouteQuery = new RouteSearch.WalkRouteQuery(
                fromAndTo, RouteSearch.WALK_DEFAULT);
        routeSearch.calculateWalkRouteAsyn(walkRouteQuery);
    }

/**
     * 计算公交路线
     */
    private void calculateBusRoute() {
        if (routeSearch == null){
            routeSearch = new RouteSearch(this);
        }
        if (latitude != -1 && longitude != -1) {
            fromAndTo = new RouteSearch.FromAndTo(new LatLonPoint(lat, lng), new LatLonPoint(latitude, longitude));
        }
        routeSearch.setRouteSearchListener(this);
//        showProgress("正在加载...");
        // fromAndTo包含路径规划的起点和终点,RouteSearch.BusLeaseWalk表示公交查询模式
        // 第三个参数表示公交查询城市区号,第四个参数表示是否计算夜班车,0表示不计算,1表示计算
        RouteSearch.BusRouteQuery busRouteQuery = new RouteSearch.BusRouteQuery(
                fromAndTo, RouteSearch.BUS_DEFAULT, "020", 1);
        routeSearch.calculateBusRouteAsyn(busRouteQuery);
    }

/**
     * 计算自驾路线
     */
    private void calculateCarRoute() {
        if (routeSearch == null){
            routeSearch = new RouteSearch(this);
        }
        if (latitude != -1 && longitude != -1) {
            fromAndTo = new RouteSearch.FromAndTo(new LatLonPoint(lat, lng), new LatLonPoint(latitude, longitude));
        }
        routeSearch.setRouteSearchListener(this);
        // fromAndTo包含路径规划的起点和终点,drivingMode表示驾车模式
        // 第三个参数表示途经点(最多支持16个),第四个参数表示避让区域(最多支持32个),第五个参数表示避让道路
        RouteSearch.DriveRouteQuery driveRouteQuery = new RouteSearch.DriveRouteQuery(
                fromAndTo, RouteSearch.DRIVING_MULTI_STRATEGY_FASTEST_SHORTEST_AVOID_CONGESTION, null, null, "");
        routeSearch.calculateDriveRouteAsyn(driveRouteQuery);
    }

/**
     * 公交车路线规划
     * @param busRouteResult
     * @param i
     */
    @Override
    public void onBusRouteSearched(BusRouteResult busRouteResult, int i) {
        if (i == 1000) {                                                                            //1000代表成功
            BusPath busPath = busRouteResult.getPaths().get(0);                                     //在地图上绘制路径:
            float distance = busPath.getDistance();                                                 //总距离
            long duration = busPath.getDuration();                                                  //总时间

            int round = Math.round(distance);
            String gl = String.valueOf(Math.round(round / 100d) / 10d);
            String normalHMTime = formatDateTime(duration);
            button_tv_top.setText("距离目的地约"+ gl+"公里,"+"公交预计"+ normalHMTime +"后到达");

            BusRouteOverlay busRouteOverlay = new BusRouteOverlay(this, aMap, busPath, busRouteResult.getStartPos(), busRouteResult.getTargetPos());
            aMap.clear();
            busRouteOverlay.removeFromMap();                                                       //去掉Overlay上所有的Marker。
            busRouteOverlay.addToMap();                                                            //将Overlay添加到地图上显示
            busRouteOverlay.zoomToSpan();                                                          //调整地图能看到起点和终点
        }
    }

/**
     * 驾车路线规划
     * @param driveRouteResult
     * @param i
     */
    @Override
    public void onDriveRouteSearched(DriveRouteResult driveRouteResult, int i) {
        if (i==1000){
            DrivePath drivePath = driveRouteResult.getPaths().get(0);
            float distance = drivePath.getDistance();                                                 //总距离
            long duration = drivePath.getDuration();                                                  //总时间

            int round = Math.round(distance);
            String gl = String.valueOf(Math.round(round / 100d) / 10d);
            String normalHMTime = formatDateTime(duration);
            button_tv_top.setText("距离目的地约"+ gl+"公里,"+"驾车预计"+ normalHMTime +"后到达");

            DrivingRouteOverlay drivingRouteOverlay = new DrivingRouteOverlay(this, aMap, drivePath, driveRouteResult.getStartPos(), driveRouteResult.getTargetPos());
            aMap.clear();
            drivingRouteOverlay.removeFromMap();
            drivingRouteOverlay.addToMap();
            drivingRouteOverlay.zoomToSpan();
        }
    }

 /**
     * 步行回调
     * @param walkRouteResult
     * @param i
     */
    @Override
    public void onWalkRouteSearched(WalkRouteResult walkRouteResult, int i) {
        if (i == 1000) {//1000代表成功
            WalkPath walkPath = walkRouteResult.getPaths().get(0);                                   //在地图上绘制路径:
            float distance = walkPath.getDistance();                                                 //总距离
            long duration = walkPath.getDuration();                                                  //总时间

            int round = Math.round(distance);
            String gl = String.valueOf(Math.round(round / 100d) / 10d);
            String normalHMTime = formatDateTime(duration);
            button_tv_top.setText("距离目的地约"+ gl+"公里,"+"步行预计"+ normalHMTime +"后到达");

            WalkRouteOverlay walkRouteOverlay = new WalkRouteOverlay(this, aMap, walkPath, walkRouteResult.getStartPos(), walkRouteResult.getTargetPos());
            aMap.clear();
            walkRouteOverlay.removeFromMap();                                                       //去掉Overlay上所有的Marker。
            walkRouteOverlay.addToMap();                                                            //将Overlay添加到地图上显示
            walkRouteOverlay.zoomToSpan();                                                          //调整地图能看到起点和终点
        }
    }

定位操作:

/**
     * init - map
     */
    private void initMap() {
        if (aMap == null) {
            aMap = mapView.getMap();
            mUiSettings = aMap.getUiSettings();
            mUiSettings.setScrollGesturesEnabled(true);
            mUiSettings.setZoomGesturesEnabled(true);
            aMap.moveCamera(CameraUpdateFactory.zoomTo(20));                                      //设置级别区间
            aMap.setOnMarkerClickListener(this);
            setUpMap();
        }
    }
    /**
     * 设置一些amap的属性
     */
    private void setUpMap() {
        MyLocationStyle myLocationStyle = new MyLocationStyle();
        myLocationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(R.drawable.location_marker));// 设置小蓝点的图标
        myLocationStyle.strokeColor(Color.BLACK);                                                        // 设置圆形的边框颜色
        myLocationStyle.radiusFillColor(Color.argb(100, 0, 0, 180));                                     // 设置圆形的填充颜色
        // myLocationStyle.anchor(int,int)                                                               //设置小蓝点的锚点
        myLocationStyle.strokeWidth(1.0f);                                                               // 设置圆形的边框粗细
        aMap.setMyLocationStyle(myLocationStyle);
        aMap.setLocationSource(this);                                                                    // 设置定位监听
        aMap.getUiSettings().setMyLocationButtonEnabled(true);                                           // 设置默认定位按钮是否显示
        aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false
        // aMap.setMyLocationType()
    }

    /***
     * 定位成功之后回调
     * @param aMapLocation
     */
    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (mListener != null && aMapLocation != null) {
            if (aMapLocation != null && aMapLocation.getErrorCode() == 0) {
                mListener.onLocationChanged(aMapLocation);// 显示系统小蓝点
            } else {
                String errText = "定位失败," + aMapLocation.getErrorCode() + ": " + aMapLocation.getErrorInfo();
                Log.e("AmapErr", errText);
            }
            this.lat=aMapLocation.getLatitude();
            this.lng=aMapLocation.getLongitude();
//            ToastUtil.showAnimToast(this,lat+ "--------"+lng);                                    //定位回调
        }
    }

other:

/**
     * 将秒数转换成 小时  和  分钟
     * @return
     */
    public static String formatDateTime(long mss) {
        String DateTimes = null;
        long days = mss / ( 60 * 60 * 24);
        long hours = (mss % ( 60 * 60 * 24)) / (60 * 60);
        long minutes = (mss % ( 60 * 60)) /60;
        long seconds = mss % 60;
        if(days>0){
            DateTimes= days + "天" + hours + "小时" + minutes + "分钟" + seconds + "秒";
        }else if(hours>0){
            DateTimes=hours + "小时" + minutes + "分钟" + seconds + "秒";
        }else if(minutes>0){
            DateTimes=minutes + "分钟" + seconds + "秒";
        }else{
            DateTimes=seconds + "秒";
        }
        return DateTimes;
    }


你可能感兴趣的:(Android,Third,Party)