安卓高德地图 - (路线规划[驾车+公交+步行+骑行5])

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34536167/article/details/79338243

1、依次为驾车、公交+步行、骑行效果图

驾车+公交

步行+骑行

2、RouteSearchActivity

2.1、

public class RouteSearchActivity extends AppCompatActivity implements RouteSearch.OnRouteSearchListener, View.OnClickListener {

    private MapView mMapView;
    private AMap aMap;
    private TextView content;

    private static final String TAG = "RouteSearchActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_route_search);

        findViewById(R.id.route_bus).setOnClickListener(this);
        findViewById(R.id.route_driver).setOnClickListener(this);
        findViewById(R.id.route_walk).setOnClickListener(this);
        findViewById(R.id.route_ride).setOnClickListener(this);
        content = (TextView) this.findViewById(R.id.route_content);
        mMapView = (MapView) this.findViewById(R.id.map);

        // 此方法须重写,虚拟机需要在很多情况下保存地图绘制的当前状态
        mMapView.onCreate(savedInstanceState);
        //默认公交
        setRouteCarListener(1);
    }

    /**
     * 点击事件
     */
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.route_bus:
                setRouteCarListener(1);
                break;
            case R.id.route_driver:
                setRouteCarListener(0);
                break;
            case R.id.route_walk:
                setRouteCarListener(2);
                break;
            case R.id.route_ride:
                setRouteCarListener(3);
                break;
        }
    }

    /**
     * 自带dialog进度
     */
    private ProgressDialog dialog;

    /**
     * 规划线路
     * type 0 驾车 1 公交 2 步行 3 骑行
     *
     * @param type
     */
    private void setRouteCarListener(int type) {
        if (aMap == null) {
            aMap = mMapView.getMap();
        }
        //清除防止重复显示
        aMap.clear();
        if (dialog == null) {
            dialog = new ProgressDialog(this);
        }
        dialog.setMessage("正在规划路线,请稍后...");
        dialog.show();

        RouteSearch routeSearch = new RouteSearch(this);
        //模拟起始点与目的经纬度(如:深圳市)
        RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(new LatLonPoint(22.5587, 113.8727),
                new LatLonPoint(22.5587, 113.8950));
        //驾车:第一个参数表示fromAndTo包含路径规划的起点和终点,drivingMode表示驾车模式(支持20种模式  -在PathPlanningStrategy类中定义)
        //第三个参数表示途经点(最多支持16个),第四个参数表示避让区域(最多支持32个),第五个参数表示避让道路
        //模式链接:http://lbs.amap.com/api/android-navi-sdk/guide/route-plan/drive-route-plan
        if (type == 0) {
            RouteSearch.DriveRouteQuery query = new RouteSearch.DriveRouteQuery(fromAndTo, PathPlanningStrategy.DRIVING_DEFAULT, null, null, "");
            routeSearch.calculateDriveRouteAsyn(query);
        } else if (type == 1) {
            //公交:fromAndTo包含路径规划的起点和终点,RouteSearch.BusLeaseWalk表示公交查询模式
            //第三个参数表示公交查询城市区号,第四个参数表示是否计算夜班车,0表示不计算,1表示计算
            RouteSearch.BusRouteQuery query1 = new RouteSearch.BusRouteQuery(fromAndTo, RouteSearch.BusLeaseWalk, "0755", 1);//深圳区号
            routeSearch.calculateBusRouteAsyn(query1);
        } else if (type == 2) {
            //步行:SDK提供两种模式:RouteSearch.WALK_DEFAULT 和 RouteSearch.WALK_MULTI_PATH(注意:过时)
            RouteSearch.WalkRouteQuery query2 = new RouteSearch.WalkRouteQuery(fromAndTo);
            routeSearch.calculateWalkRouteAsyn(query2);
        } else if (type == 3) {
            //骑行:(默认推荐路线及最快路线综合模式,可以接二参同上)
            RouteSearch.RideRouteQuery query3 = new RouteSearch.RideRouteQuery(fromAndTo);
            routeSearch.calculateRideRouteAsyn(query3);
        }
        routeSearch.setRouteSearchListener(this);
    }

    @Override
    public void onBusRouteSearched(BusRouteResult busRouteResult, int i) {
        dialog.dismiss();
        if (i == 1000) {
            BusPath busPath = busRouteResult.getPaths().get(0);
            setBusRoute(busPath, busRouteResult.getStartPos(), busRouteResult.getTargetPos());
            float distance = busPath.getDistance() / 1000;
            long duration = busPath.getDuration() / 60;
            //需步行距离
            float walkdistance = busPath.getWalkDistance() / 1000;
            //行车的距离
            float busdistance = busPath.getBusDistance() / 1000;
            //成本、费用(其中walkdistance+busdistance=distance 行车+步行=总距离)
            float cost = busPath.getCost();
            content.setText("\n距离/公里:" + distance + "\n时间/分:" + duration + "\n步行距离/公里:" + walkdistance
                    + "\n行车距离/公里:" + busdistance + "\n成本、费用:" + cost);
        } else {
            Log.e(TAG, "onBusRouteSearched: 路线规划失败");
        }
    }

    @Override
    public void onDriveRouteSearched(DriveRouteResult driveRouteResult, int i) {
        dialog.dismiss();
        if (i == 1000) {
            DrivePath drivePath = driveRouteResult.getPaths().get(0);
            setDrivingRoute(drivePath, driveRouteResult.getStartPos(), driveRouteResult.getTargetPos());
            //策略
            String strategy = drivePath.getStrategy();
            //总的交通信号灯数
            int clights = drivePath.getTotalTrafficlights();
            //距离 米:/1000转公里 1公里=1km
            float distance = drivePath.getDistance() / 1000;
            //时间 秒:、60转分
            long duration = drivePath.getDuration() / 60;
            content.setText("策略:" + strategy + "\n总的交通信号灯数/个:" + clights +
                    "\n距离/公里:" + distance + "\n时间/分:" + duration);
        } else {
            Log.e(TAG, "onDriveRouteSearched: 路线规划失败");
        }
    }

    @Override
    public void onWalkRouteSearched(WalkRouteResult walkRouteResult, int i) {
        dialog.dismiss();
        if (i == 1000) {
            WalkPath walkPath = walkRouteResult.getPaths().get(0);
            setWalkRoute(walkPath, walkRouteResult.getStartPos(), walkRouteResult.getTargetPos());
            float distance = walkPath.getDistance() / 1000;
            long duration = walkPath.getDuration() / 60;
            content.setText("\n距离/公里:" + distance + "\n时间/分:" + duration);
        } else {
            Log.e(TAG, "onWalkRouteSearched: 路线规划失败");
        }
    }

    @Override
    public void onRideRouteSearched(RideRouteResult rideRouteResult, int i) {
        dialog.dismiss();
        if (i == 1000) {
            RidePath ridePath = rideRouteResult.getPaths().get(0);
            setRideRoute(ridePath, rideRouteResult.getStartPos(), rideRouteResult.getTargetPos());
            float distance = ridePath.getDistance() / 1000;
            long duration = ridePath.getDuration() / 60;
            content.setText("\n距离/公里:" + distance + "\n时间/分:" + duration);
        } else {
            Log.e(TAG, "onRideRouteSearched: 路线规划失败");
        }
    }

    /**
     * 驾车线路
     */
    private void setDrivingRoute(DrivePath drivePath, LatLonPoint start, LatLonPoint end) {
        DrivingRouteOverlay drivingRouteOverlay = new DrivingRouteOverlay(this, aMap, drivePath, start, end);
        drivingRouteOverlay.setNodeIconVisibility(true);//设置节点(转弯)marker是否显示
        drivingRouteOverlay.setIsColorfulline(true);//是否用颜色展示交通拥堵情况,默认true
        drivingRouteOverlay.removeFromMap();//去掉DriveLineOverlay上的线段和标记。
        drivingRouteOverlay.addToMap(); //添加驾车路线添加到地图上显示。
        drivingRouteOverlay.zoomToSpan();//移动镜头到当前的视角。
        drivingRouteOverlay.setRouteWidth(1);//设置路线的宽度
    }

    /**
     * 公交规划线路
     */
    private void setBusRoute(BusPath busPath, LatLonPoint start, LatLonPoint end) {
        BusRouteOverlay busRouteOverlay = new BusRouteOverlay(this, aMap, busPath, start, end);
        busRouteOverlay.removeFromMap();//去掉DriveLineOverlay上的线段和标记。
        busRouteOverlay.addToMap(); //添加驾车路线添加到地图上显示。
        busRouteOverlay.zoomToSpan();//移动镜头到当前的视角。
        busRouteOverlay.setNodeIconVisibility(true);//是否显示路段节点图标

    }

    /**
     * 步行规划线路
     */
    private void setWalkRoute(WalkPath walkPath, LatLonPoint start, LatLonPoint end) {
        WalkRouteOverlay walkRouteOverlay = new WalkRouteOverlay(this, aMap, walkPath, start, end);
        walkRouteOverlay.removeFromMap();
        walkRouteOverlay.addToMap();
        walkRouteOverlay.zoomToSpan();
    }

    /**
     * 骑行规划线路
     */
    private void setRideRoute(RidePath ridePath, LatLonPoint start, LatLonPoint end) {
        RideRouteOverlay rideRouteOverlay = new RideRouteOverlay(this, aMap, ridePath, start, end);
        rideRouteOverlay.removeFromMap();
        rideRouteOverlay.addToMap();
        rideRouteOverlay.zoomToSpan();
    }
}

2.2、布局activity_route_search

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.amap.api.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <LinearLayout
        android:background="#88000000"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <Button
                android:id="@+id/route_bus"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="公交"
                android:textSize="20sp" />

            <Button
                android:id="@+id/route_driver"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="驾车"
                android:textSize="20sp" />

            <Button
                android:id="@+id/route_walk"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="步行"
                android:textSize="20sp" />

            <Button
                android:id="@+id/route_ride"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="骑行"
                android:textSize="20sp" />
        </LinearLayout>

        <TextView
            android:textColor="#fff"
            android:id="@+id/route_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="20sp" />
    </LinearLayout>
</RelativeLayout>

2.3、其中关键类代码官网已给出,解压AMap_Android_API_3DMap_Demo,在进入下图路径下的overlay文件夹拷贝即可,在这里也直接发出来,只提取关键类并改动了部分代码

这里写图片描述

公交路线图层类:

/**
 * 公交路线图层类。在高德地图API里,如果需要显示公交路线,可以用此类来创建公交路线图层。如不满足需求,也可以自己创建自定义的公交路线图层。
 * @since V2.1.0
 */
public class BusRouteOverlay extends RouteOverlay {

    private BusPath busPath;
    private LatLng latLng;

    /**
     * 通过此构造函数创建公交路线图层。
     * @param context 当前activity。
     * @param amap 地图对象。
     * @param path 公交路径规划的一个路段。详见搜索服务模块的路径查询包(com.amap.api.services.route)中的类-BusPath
     * @param start 起点坐标。详见搜索服务模块的核心基础包(com.amap.api.services.core)中的类 -LatLonPoint
     * @param end 终点坐标。详见搜索服务模块的核心基础包(com.amap.api.services.core)中的类 -LatLonPoint
     * @since V2.1.0
     */
    public BusRouteOverlay(Context context, AMap amap, BusPath path,
            LatLonPoint start, LatLonPoint end) {
        super(context);
        this.busPath = path;
        startPoint = AMapUtil.convertToLatLng(start);
        endPoint = AMapUtil.convertToLatLng(end);
        mAMap = amap;
    }

    /**
     * 添加公交路线到地图上。
     * @since V2.1.0
     */

    public void addToMap() {
        /**
         * 绘制节点和线<br>
         * 细节情况较多<br>
         * 两个step之间,用step和step1区分<br>
         * 1.一个step内可能有步行和公交,然后有可能他们之间连接有断开<br>
         * 2.step的公交和step1的步行,有可能连接有断开<br>
         * 3.step和step1之间是公交换乘,且没有步行,需要把step的终点和step1的起点连起来<br>
         * 4.公交最后一站和终点间有步行,加入步行线路,还会有一些步行marker<br>
         * 5.公交最后一站和终点间无步行,之间连起来<br>
         */
        try {
            List<BusStep> busSteps = busPath.getSteps();
            for (int i = 0; i < busSteps.size(); i++) {
                BusStep busStep = busSteps.get(i);
                if (i < busSteps.size() - 1) {
                    BusStep busStep1 = busSteps.get(i + 1);// 取得当前下一个BusStep对象
                    // 假如步行和公交之间连接有断开,就把步行最后一个经纬度点和公交第一个经纬度点连接起来,避免断线问题
                    if (busStep.getWalk() != null
                            && busStep.getBusLine() != null) {
                        checkWalkToBusline(busStep);
                    }

                    // 假如公交和步行之间连接有断开,就把上一公交经纬度点和下一步行第一个经纬度点连接起来,避免断线问题
                    if (busStep.getBusLine() != null
                            && busStep1.getWalk() != null 
                            && busStep1.getWalk().getSteps().size() > 0) {
                        checkBusLineToNextWalk(busStep, busStep1);
                    }
                    // 假如两个公交换乘中间没有步行,就把上一公交经纬度点和下一步公交第一个经纬度点连接起来,避免断线问题
                    if (busStep.getBusLine() != null
                            && busStep1.getWalk() == null
                            && busStep1.getBusLine() != null) {
                        checkBusEndToNextBusStart(busStep, busStep1);
                    }
                    // 和上面的很类似
                    if (busStep.getBusLine() != null
                            && busStep1.getWalk() == null
                            && busStep1.getBusLine() != null) {
                        checkBusToNextBusNoWalk(busStep, busStep1);
                    }
                    if (busStep.getBusLine() != null
                            && busStep1.getRailway() != null ) {
                        checkBusLineToNextRailway(busStep, busStep1);
                    }
                    if (busStep1.getWalk() != null &&
                            busStep1.getWalk().getSteps().size() > 0 &&
                            busStep.getRailway() != null) {
                        checkRailwayToNextWalk(busStep, busStep1);
                    }

                    if ( busStep1.getRailway() != null &&
                            busStep.getRailway() != null) {
                        checkRailwayToNextRailway(busStep, busStep1);
                    }

                    if (busStep.getRailway() != null && 
                        busStep1.getTaxi() != null ){
                        checkRailwayToNextTaxi(busStep, busStep1);
                    }
                }

                if (busStep.getWalk() != null
                        && busStep.getWalk().getSteps().size() > 0) {
                    addWalkSteps(busStep);
                } else {
                    if (busStep.getBusLine() == null && busStep.getRailway() == null && busStep.getTaxi() == null) {
                        addWalkPolyline(latLng, endPoint);
                    }
                }
                if (busStep.getBusLine() != null) {
                    RouteBusLineItem routeBusLineItem = busStep.getBusLine();
                    addBusLineSteps(routeBusLineItem);
                    addBusStationMarkers(routeBusLineItem);
                    if (i == busSteps.size() - 1) {
                        addWalkPolyline(AMapUtil.convertToLatLng(getLastBuslinePoint(busStep)), endPoint);
                    }
                }
                if (busStep.getRailway() != null) {
                    addRailwayStep(busStep.getRailway());
                    addRailwayMarkers(busStep.getRailway());
                    if (i == busSteps.size() - 1) {
                        addWalkPolyline(AMapUtil.convertToLatLng(busStep.getRailway().getArrivalstop().getLocation()), endPoint);
                    }
                }
                if (busStep.getTaxi() != null) {
                    addTaxiStep(busStep.getTaxi());
                    addTaxiMarkers(busStep.getTaxi());
                }
            }
            addStartAndEndMarker();

        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    private void checkRailwayToNextTaxi(BusStep busStep, BusStep busStep1) {
        LatLonPoint railwayLastPoint = busStep.getRailway().getArrivalstop().getLocation();
        LatLonPoint taxiFirstPoint = busStep1.getTaxi().getOrigin();
        if (!railwayLastPoint.equals(taxiFirstPoint)) {
            addWalkPolyLineByLatLonPoints(railwayLastPoint, taxiFirstPoint);
        }
    }

    private void checkRailwayToNextRailway(BusStep busStep, BusStep busStep1) {
        LatLonPoint railwayLastPoint = busStep.getRailway().getArrivalstop().getLocation();
        LatLonPoint railwayFirstPoint = busStep1.getRailway().getDeparturestop().getLocation();
        if (!railwayLastPoint.equals(railwayFirstPoint)) {
            addWalkPolyLineByLatLonPoints(railwayLastPoint, railwayFirstPoint);
        }

    }

    private void checkBusLineToNextRailway(BusStep busStep, BusStep busStep1) {
        LatLonPoint busLastPoint = getLastBuslinePoint(busStep);
        LatLonPoint railwayFirstPoint = busStep1.getRailway().getDeparturestop().getLocation();
        if (!busLastPoint.equals(railwayFirstPoint)) {
            addWalkPolyLineByLatLonPoints(busLastPoint, railwayFirstPoint);
        }

    }

    private void checkRailwayToNextWalk(BusStep busStep, BusStep busStep1) {
        LatLonPoint railwayLastPoint = busStep.getRailway().getArrivalstop().getLocation();
        LatLonPoint walkFirstPoint = getFirstWalkPoint(busStep1);
        if (!railwayLastPoint.equals(walkFirstPoint)) {
            addWalkPolyLineByLatLonPoints(railwayLastPoint, walkFirstPoint);
        }

    }

    private void addRailwayStep(RouteRailwayItem railway) {
        List<LatLng> railwaylistpoint = new ArrayList<LatLng>();
        List<RailwayStationItem> railwayStationItems = new ArrayList<RailwayStationItem>();
        railwayStationItems.add(railway.getDeparturestop());
        railwayStationItems.addAll(railway.getViastops());
        railwayStationItems.add(railway.getArrivalstop());
        for (int i = 0; i < railwayStationItems.size(); i++) {
            railwaylistpoint.add(AMapUtil.convertToLatLng(railwayStationItems.get(i).getLocation()));
        }
        addRailwayPolyline(railwaylistpoint);
    }

    private void addTaxiStep(TaxiItem taxi){
        addPolyLine(new PolylineOptions().width(getRouteWidth())
                .color(getBusColor())
                .add(AMapUtil.convertToLatLng(taxi.getOrigin()))
                .add(AMapUtil.convertToLatLng(taxi.getDestination())));
    }

    /**
     * @param busStep
     */
    private void addWalkSteps(BusStep busStep) {
        RouteBusWalkItem routeBusWalkItem = busStep.getWalk();
        List<WalkStep> walkSteps = routeBusWalkItem.getSteps();
        for (int j = 0; j < walkSteps.size(); j++) {
            WalkStep walkStep = walkSteps.get(j);
            if (j == 0) {
                LatLng latLng = AMapUtil.convertToLatLng(walkStep
                        .getPolyline().get(0));
                String road = walkStep.getRoad();// 道路名字
                String instruction = getWalkSnippet(walkSteps);// 步行导航信息
                addWalkStationMarkers(latLng, road, instruction);
            }

            List<LatLng> listWalkPolyline = AMapUtil
                    .convertArrList(walkStep.getPolyline());
            this.latLng = listWalkPolyline.get(listWalkPolyline.size() - 1);
            addWalkPolyline(listWalkPolyline);

            // 假如步行前一段的终点和下的起点有断开,断画直线连接起来,避免断线问题
            if (j < walkSteps.size() - 1) {
                LatLng lastLatLng = listWalkPolyline.get(listWalkPolyline
                        .size() - 1);
                LatLng firstlatLatLng = AMapUtil
                        .convertToLatLng(walkSteps.get(j + 1).getPolyline()
                                .get(0));
                if (!(lastLatLng.equals(firstlatLatLng))) {
                    addWalkPolyline(lastLatLng, firstlatLatLng);
                }
            }
        }
    }

    /**
     * 添加一系列的bus PolyLine
     *
     * @param routeBusLineItem
     */
    private void addBusLineSteps(RouteBusLineItem routeBusLineItem) {
        addBusLineSteps(routeBusLineItem.getPolyline());
    }

    private void addBusLineSteps(List<LatLonPoint> listPoints) {
        if (listPoints.size() < 1) {
            return;
        }
        addPolyLine(new PolylineOptions().width(getRouteWidth())
                .color(getBusColor())
                .addAll(AMapUtil.convertArrList(listPoints)));
    }

    /**
     * 添加 Markers
     * @param latLng
     *  marker
     * @param title
     * @param snippet
     */
    private void addWalkStationMarkers(LatLng latLng, String title,
            String snippet) {
        addStationMarker(new MarkerOptions().position(latLng).title(title)
                .snippet(snippet).anchor(0.5f, 0.5f).visible(nodeIconVisible)
                .icon(getWalkBitmapDescriptor()));
    }

    /**
     * @param routeBusLineItem
     */
    private void addBusStationMarkers(RouteBusLineItem routeBusLineItem) {
        BusStationItem startBusStation = routeBusLineItem
                .getDepartureBusStation();
        LatLng position = AMapUtil.convertToLatLng(startBusStation
                .getLatLonPoint());
        String title = routeBusLineItem.getBusLineName();
        String snippet = getBusSnippet(routeBusLineItem);

        addStationMarker(new MarkerOptions().position(position).title(title)
                .snippet(snippet).anchor(0.5f, 0.5f).visible(nodeIconVisible)
                .icon(getBusBitmapDescriptor()));
    }

    private void addTaxiMarkers(TaxiItem taxiItem) {

        LatLng position = AMapUtil.convertToLatLng(taxiItem
                .getOrigin());
        String title = taxiItem.getmSname()+"打车";
        String snippet = "到终点";

        addStationMarker(new MarkerOptions().position(position).title(title)
                .snippet(snippet).anchor(0.5f, 0.5f).visible(nodeIconVisible)
                .icon(getDriveBitmapDescriptor()));
    }

    private void addRailwayMarkers(RouteRailwayItem railway) {
        LatLng Departureposition = AMapUtil.convertToLatLng(railway
                .getDeparturestop().getLocation());
        String Departuretitle = railway.getDeparturestop().getName()+"上车";
        String Departuresnippet = railway.getName();

        addStationMarker(new MarkerOptions().position(Departureposition).title(Departuretitle)
                .snippet(Departuresnippet).anchor(0.5f, 0.5f).visible(nodeIconVisible)
                .icon(getBusBitmapDescriptor()));

        LatLng Arrivalposition = AMapUtil.convertToLatLng(railway
                .getArrivalstop().getLocation());
        String Arrivaltitle = railway.getArrivalstop().getName()+"下车";
        String Arrivalsnippet = railway.getName();

        addStationMarker(new MarkerOptions().position(Arrivalposition).title(Arrivaltitle)
                .snippet(Arrivalsnippet).anchor(0.5f, 0.5f).visible(nodeIconVisible)
                .icon(getBusBitmapDescriptor()));
    }
    /**
     * 如果换乘没有步行 检查bus最后一点和下一个step的bus起点是否一致
     *
     * @param busStep
     * @param busStep1
     */
    private void checkBusToNextBusNoWalk(BusStep busStep, BusStep busStep1) {
        LatLng endbusLatLng = AMapUtil
                .convertToLatLng(getLastBuslinePoint(busStep));
        LatLng startbusLatLng = AMapUtil
                .convertToLatLng(getFirstBuslinePoint(busStep1));
        if (startbusLatLng.latitude - endbusLatLng.latitude > 0.0001
                || startbusLatLng.longitude - endbusLatLng.longitude > 0.0001) {
            drawLineArrow(endbusLatLng, startbusLatLng);// 断线用带箭头的直线连?
        }
    }

    /**
     *
     * checkBusToNextBusNoWalk 和这个类似
     *
     * @param busStep
     * @param busStep1
     */
    private void checkBusEndToNextBusStart(BusStep busStep, BusStep busStep1) {
        LatLonPoint busLastPoint = getLastBuslinePoint(busStep);
        LatLng endbusLatLng = AMapUtil.convertToLatLng(busLastPoint);
        LatLonPoint busFirstPoint = getFirstBuslinePoint(busStep1);
        LatLng startbusLatLng = AMapUtil.convertToLatLng(busFirstPoint);
        if (!endbusLatLng.equals(startbusLatLng)) {
            drawLineArrow(endbusLatLng, startbusLatLng);//
        }
    }

    /**
     * 检查bus最后一步和下一各step的步行起点是否一致
     *
     * @param busStep
     * @param busStep1
     */
    private void checkBusLineToNextWalk(BusStep busStep, BusStep busStep1) {
        LatLonPoint busLastPoint = getLastBuslinePoint(busStep);
        LatLonPoint walkFirstPoint = getFirstWalkPoint(busStep1);
        if (!busLastPoint.equals(walkFirstPoint)) {
            addWalkPolyLineByLatLonPoints(busLastPoint, walkFirstPoint);
        }
    }

    /**
     * 检查 步行最后一点 和 bus的起点 是否一致
     *
     * @param busStep
     */
    private void checkWalkToBusline(BusStep busStep) {
        LatLonPoint walkLastPoint = getLastWalkPoint(busStep);
        LatLonPoint buslineFirstPoint = getFirstBuslinePoint(busStep);

        if (!walkLastPoint.equals(buslineFirstPoint)) {
            addWalkPolyLineByLatLonPoints(walkLastPoint, buslineFirstPoint);
        }
    }

    /**
     * @param busStep1
     * @return
     */
    private LatLonPoint getFirstWalkPoint(BusStep busStep1) {
        return busStep1.getWalk().getSteps().get(0).getPolyline().get(0);
    }

    private void addWalkPolyLineByLatLonPoints(LatLonPoint pointFrom,
            LatLonPoint pointTo) {
        LatLng latLngFrom = AMapUtil.convertToLatLng(pointFrom);
        LatLng latLngTo = AMapUtil.convertToLatLng(pointTo);

        addWalkPolyline(latLngFrom, latLngTo);
    }

    /**
     * @param latLngFrom
     * @param latLngTo
     * @return
     */
    private void addWalkPolyline(LatLng latLngFrom, LatLng latLngTo) {
        addPolyLine(new PolylineOptions().add(latLngFrom, latLngTo)
                .width(getRouteWidth()).color(getWalkColor()).setDottedLine(true));
    }

    /**
     * @param listWalkPolyline
     */
    private void addWalkPolyline(List<LatLng> listWalkPolyline) {

        addPolyLine(new PolylineOptions().addAll(listWalkPolyline)
                .color(getWalkColor()).width(getRouteWidth()).setDottedLine(true));
    }

    private void addRailwayPolyline(List<LatLng> listPolyline) {

        addPolyLine(new PolylineOptions().addAll(listPolyline)
                .color(getDriveColor()).width(getRouteWidth()));
    }

    private String getWalkSnippet(List<WalkStep> walkSteps) {
        float disNum = 0;
        for (WalkStep step : walkSteps) {
            disNum += step.getDistance();
        }
        return "\u6B65\u884C" + disNum + "\u7C73";
    }

    public void drawLineArrow(LatLng latLngFrom, LatLng latLngTo) {

        addPolyLine(new PolylineOptions().add(latLngFrom, latLngTo).width(3)
                .color(getBusColor()).width(getRouteWidth()));// 绘制直线
    }

    private String getBusSnippet(RouteBusLineItem routeBusLineItem) {
        return "("
                + routeBusLineItem.getDepartureBusStation().getBusStationName()
                + "-->"
                + routeBusLineItem.getArrivalBusStation().getBusStationName()
                + ") \u7ECF\u8FC7" + (routeBusLineItem.getPassStationNum() + 1)
                + "\u7AD9";
    }

    /**
     * @param busStep
     * @return
     */
    private LatLonPoint getLastWalkPoint(BusStep busStep) {

        List<WalkStep> walkSteps = busStep.getWalk().getSteps();
        WalkStep walkStep = walkSteps.get(walkSteps.size() - 1);
        List<LatLonPoint> lonPoints = walkStep.getPolyline();
        return lonPoints.get(lonPoints.size() - 1);
    }

    private LatLonPoint getExitPoint(BusStep busStep) {
        Doorway doorway = busStep.getExit();
        if (doorway == null) {
            return null;
        }
        return doorway.getLatLonPoint();
    }

    private LatLonPoint getLastBuslinePoint(BusStep busStep) {
        List<LatLonPoint> lonPoints = busStep.getBusLine().getPolyline();

        return lonPoints.get(lonPoints.size() - 1);
    }

    private LatLonPoint getEntrancePoint(BusStep busStep) {
        Doorway doorway = busStep.getEntrance();
        if (doorway == null) {
            return null;
        }
        return doorway.getLatLonPoint();
    }

    private LatLonPoint getFirstBuslinePoint(BusStep busStep) {
        return busStep.getBusLine().getPolyline().get(0);
    }
}

驾车路线图层类:

/**
 * 驾车路线图层类:
 */
public class DrivingRouteOverlay extends RouteOverlay {

    private DrivePath drivePath;
    private List<LatLonPoint> throughPointList;
    private List<Marker> throughPointMarkerList = new ArrayList<Marker>();
    private boolean throughPointMarkerVisible = true;
    private List<TMC> tmcs;
    private PolylineOptions mPolylineOptions;
    private PolylineOptions mPolylineOptionscolor;
    private Context mContext;
    private boolean isColorfulline = true;
    private float mWidth = 25;
    private List<LatLng> mLatLngsOfPath;

    public void setIsColorfulline(boolean iscolorfulline) {
        this.isColorfulline = iscolorfulline;
    }

    /**
     * 根据给定的参数,构造一个导航路线图层类对象。
     *
     * @param amap    地图对象。
     * @param path    导航路线规划方案。
     * @param context 当前的activity对象。
     */
    public DrivingRouteOverlay(Context context, AMap amap, DrivePath path,
                               LatLonPoint start, LatLonPoint end) {
        super(context);
        mContext = context;
        mAMap = amap;
        this.drivePath = path;
        startPoint = AMapUtil.convertToLatLng(start);
        endPoint = AMapUtil.convertToLatLng(end);
    }

    public float getRouteWidth() {
        return mWidth;
    }

    /**
     * 设置路线宽度
     *
     * @param mWidth 路线宽度,取值范围:大于0
     */
    public void setRouteWidth(float mWidth) {
        this.mWidth = mWidth;
    }

    /**
     * 添加驾车路线添加到地图上显示。
     */
    public void addToMap() {
        initPolylineOptions();
        try {
            if (mAMap == null) {
                return;
            }
            if (mWidth == 0 || drivePath == null) {
                return;
            }
            mLatLngsOfPath = new ArrayList<LatLng>();
            tmcs = new ArrayList<TMC>();
            List<DriveStep> drivePaths = drivePath.getSteps();
            mPolylineOptions.add(startPoint);
            for (DriveStep step : drivePaths) {
                List<LatLonPoint> latlonPoints = step.getPolyline();
                List<TMC> tmclist = step.getTMCs();
                tmcs.addAll(tmclist);
                addDrivingStationMarkers(step, convertToLatLng(latlonPoints.get(0)));
                for (LatLonPoint latlonpoint : latlonPoints) {
                    mPolylineOptions.add(convertToLatLng(latlonpoint));
                    mLatLngsOfPath.add(convertToLatLng(latlonpoint));
                }
            }
            mPolylineOptions.add(endPoint);
            if (startMarker != null) {
                startMarker.remove();
                startMarker = null;
            }
            if (endMarker != null) {
                endMarker.remove();
                endMarker = null;
            }
            addStartAndEndMarker();
            addThroughPointMarker();
            if (isColorfulline && tmcs.size() > 0) {
                colorWayUpdate(tmcs);
                showcolorPolyline();
            } else {
                showPolyline();
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    /**
     * 初始化线段属性
     */
    private void initPolylineOptions() {
        mPolylineOptions = null;
        mPolylineOptions = new PolylineOptions();
        mPolylineOptions.color(getDriveColor()).width(getRouteWidth());
    }

    private void showPolyline() {
        addPolyLine(mPolylineOptions);
    }

    private void showcolorPolyline() {
        addPolyLine(mPolylineOptionscolor);
    }

    /**
     * 根据不同的路段拥堵情况展示不同的颜色
     *
     * @param tmcSection
     */
    private void colorWayUpdate(List<TMC> tmcSection) {
        if (mAMap == null) {
            return;
        }
        if (tmcSection == null || tmcSection.size() <= 0) {
            return;
        }
        TMC segmentTrafficStatus;
        mPolylineOptionscolor = null;
        mPolylineOptionscolor = new PolylineOptions();
        mPolylineOptionscolor.width(getRouteWidth());
        List<Integer> colorList = new ArrayList<Integer>();
        mPolylineOptionscolor.add(startPoint);
        mPolylineOptionscolor.add(AMapUtil.convertToLatLng(tmcSection.get(0).getPolyline().get(0)));
        colorList.add(getDriveColor());
        for (int i = 0; i < tmcSection.size(); i++) {
            segmentTrafficStatus = tmcSection.get(i);
            int color = getcolor(segmentTrafficStatus.getStatus());
            List<LatLonPoint> mployline = segmentTrafficStatus.getPolyline();
            for (int j = 1; j < mployline.size(); j++) {
                mPolylineOptionscolor.add(AMapUtil.convertToLatLng(mployline.get(j)));
                colorList.add(color);
            }
        }
        mPolylineOptionscolor.add(endPoint);
        colorList.add(getDriveColor());
        mPolylineOptionscolor.colorValues(colorList);
    }

    /**
     * 根据路况设置不同的颜色路线
     *
     * @param status
     * @return
     */
    private int getcolor(String status) {
        if (status.equals("畅通")) {
            return Color.GREEN;
        } else if (status.equals("缓行")) {
            return Color.YELLOW;
        } else if (status.equals("拥堵")) {
            return Color.RED;
        } else if (status.equals("严重拥堵")) {
            return Color.parseColor("#990033");
        } else {
            return Color.parseColor("#477cee");
        }
    }

    public LatLng convertToLatLng(LatLonPoint point) {
        return new LatLng(point.getLatitude(), point.getLongitude());
    }

    /**
     * 转弯处的market
     * @param driveStep
     * @param latLng
     */
    private void addDrivingStationMarkers(DriveStep driveStep, LatLng latLng) {
        String content = "";
        String snippet = driveStep.getInstruction();
        if (snippet.length() > 10) {
            String str = snippet.substring(0, 9);
            String str1 = snippet.substring(9, snippet.length());
            content = str + "\n" + str1;
        } else if (snippet.length() < 10) {
            content = snippet;
        }
        addStationMarker(new MarkerOptions()
                .position(latLng)
                .title("\u65B9\u5411:" + driveStep.getAction()
                        + "\n\u9053\u8DEF:" + driveStep.getRoad())
                .snippet(content).visible(nodeIconVisible)
                .anchor(0.5f, 0.5f).icon(getDriveBitmapDescriptor()));
    }

    @Override
    protected LatLngBounds getLatLngBounds() {
        LatLngBounds.Builder b = LatLngBounds.builder();
        b.include(new LatLng(startPoint.latitude, startPoint.longitude));
        b.include(new LatLng(endPoint.latitude, endPoint.longitude));
        if (this.throughPointList != null && this.throughPointList.size() > 0) {
            for (int i = 0; i < this.throughPointList.size(); i++) {
                b.include(new LatLng(
                        this.throughPointList.get(i).getLatitude(),
                        this.throughPointList.get(i).getLongitude()));
            }
        }
        return b.build();
    }

    public void setThroughPointIconVisibility(boolean visible) {
        try {
            throughPointMarkerVisible = visible;
            if (this.throughPointMarkerList != null
                    && this.throughPointMarkerList.size() > 0) {
                for (int i = 0; i < this.throughPointMarkerList.size(); i++) {
                    this.throughPointMarkerList.get(i).setVisible(visible);
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    private void addThroughPointMarker() {
        if (this.throughPointList != null && this.throughPointList.size() > 0) {
            LatLonPoint latLonPoint = null;
            for (int i = 0; i < this.throughPointList.size(); i++) {
                latLonPoint = this.throughPointList.get(i);
                if (latLonPoint != null) {
                    throughPointMarkerList.add(mAMap
                            .addMarker((new MarkerOptions())
                                    .position(
                                            new LatLng(latLonPoint
                                                    .getLatitude(), latLonPoint
                                                    .getLongitude()))
                                    .visible(throughPointMarkerVisible)
                                    .icon(getThroughPointBitDes())
                                    .title("\u9014\u7ECF\u70B9")));
                }
            }
        }
    }

    private BitmapDescriptor getThroughPointBitDes() {
        return BitmapDescriptorFactory.fromResource(R.mipmap.car);

    }

    /**
     * 获取两点间距离
     *
     * @param start
     * @param end
     * @return
     */
    public static int calculateDistance(LatLng start, LatLng end) {
        double x1 = start.longitude;
        double y1 = start.latitude;
        double x2 = end.longitude;
        double y2 = end.latitude;
        return calculateDistance(x1, y1, x2, y2);
    }

    public static int calculateDistance(double x1, double y1, double x2, double y2) {
        final double NF_pi = 0.01745329251994329; // 弧度 PI/180
        x1 *= NF_pi;
        y1 *= NF_pi;
        x2 *= NF_pi;
        y2 *= NF_pi;
        double sinx1 = Math.sin(x1);
        double siny1 = Math.sin(y1);
        double cosx1 = Math.cos(x1);
        double cosy1 = Math.cos(y1);
        double sinx2 = Math.sin(x2);
        double siny2 = Math.sin(y2);
        double cosx2 = Math.cos(x2);
        double cosy2 = Math.cos(y2);
        double[] v1 = new double[3];
        v1[0] = cosy1 * cosx1 - cosy2 * cosx2;
        v1[1] = cosy1 * sinx1 - cosy2 * sinx2;
        v1[2] = siny1 - siny2;
        double dist = Math.sqrt(v1[0] * v1[0] + v1[1] * v1[1] + v1[2] * v1[2]);

        return (int) (Math.asin(dist / 2) * 12742001.5798544);
    }


    //获取指定两点之间固定距离点
    public static LatLng getPointForDis(LatLng sPt, LatLng ePt, double dis) {
        double lSegLength = calculateDistance(sPt, ePt);
        double preResult = dis / lSegLength;
        return new LatLng((ePt.latitude - sPt.latitude) * preResult + sPt.latitude, (ePt.longitude - sPt.longitude) * preResult + sPt.longitude);
    }

    /**
     * 去掉DriveLineOverlay上的线段和标记。
     */
    @Override
    public void removeFromMap() {
        try {
            super.removeFromMap();
            if (this.throughPointMarkerList != null
                    && this.throughPointMarkerList.size() > 0) {
                for (int i = 0; i < this.throughPointMarkerList.size(); i++) {
                    this.throughPointMarkerList.get(i).remove();
                }
                this.throughPointMarkerList.clear();
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}

骑行路线图层类:

/**
 * 骑行路线图层类。在高德地图API里,如果要显示步行路线规划,可以用此类来创建骑行路线图层。如不满足需求,也可以自己创建自定义的骑行路线图层。
 * @since V3.5.0
 */
public class RideRouteOverlay extends RouteOverlay {

    private PolylineOptions mPolylineOptions;

    private BitmapDescriptor walkStationDescriptor= null;

    private RidePath ridePath;
    /**
     * 通过此构造函数创建骑行路线图层。
     * @param context 当前activity。
     * @param amap 地图对象。
     * @param path 骑行路线规划的一个方案。详见搜索服务模块的路径查询包(com.amap.api.services.route)中的类 -WalkStep-
     * @param start 起点。详见搜索服务模块的核心基础包(com.amap.api.services.core)中的类-LatLonPoint-
     * @param end 终点。详见搜索服务模块的核心基础包(com.amap.api.services.core)中的类-LatLonPoint-
     * @since V3.5.0
     */
    public RideRouteOverlay(Context context, AMap amap, RidePath path,
                            LatLonPoint start, LatLonPoint end) {
        super(context);
        this.mAMap = amap;
        this.ridePath = path;
        startPoint = AMapUtil.convertToLatLng(start);
        endPoint = AMapUtil.convertToLatLng(end);
    }
    /**
     * 添加骑行路线到地图中。
     * @since V3.5.0
     */
    public void addToMap() {

        initPolylineOptions();
        try {
            List<RideStep> ridePaths = ridePath.getSteps();
            mPolylineOptions.add(startPoint);
            for (int i = 0; i < ridePaths.size(); i++) {
                RideStep rideStep = ridePaths.get(i);
                LatLng latLng = AMapUtil.convertToLatLng(rideStep
                        .getPolyline().get(0));
                addRideStationMarkers(rideStep, latLng);
                addRidePolyLines(rideStep);
            }
            mPolylineOptions.add(endPoint);
            addStartAndEndMarker();
            showPolyline();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }


    /**
     * @param rideStep
     */
    private void addRidePolyLines(RideStep rideStep) {
        mPolylineOptions.addAll(AMapUtil.convertArrList(rideStep.getPolyline()));
    }
    /**
     * @param rideStep
     * @param position
     */
    private void addRideStationMarkers(RideStep rideStep, LatLng position) {
        addStationMarker(new MarkerOptions()
                .position(position)
                .title("\u65B9\u5411:" + rideStep.getAction()
                        + "\n\u9053\u8DEF:" + rideStep.getRoad())
                .snippet(rideStep.getInstruction()).visible(nodeIconVisible)
                .anchor(0.5f, 0.5f).icon(walkStationDescriptor));
    }

     /**
     * 初始化线段属性
     */
    private void initPolylineOptions() {

        if(walkStationDescriptor == null) {
            walkStationDescriptor = BitmapDescriptorFactory.fromResource(R.mipmap.man);
        }
        mPolylineOptions = null;
        mPolylineOptions = new PolylineOptions();
        mPolylineOptions.color(getDriveColor()).width(getRouteWidth());
    }
     private void showPolyline() {
            addPolyLine(mPolylineOptions);
        }
}

步行路线图层类:

/**
 * 步行路线图层类。在高德地图API里,如果要显示步行路线规划,可以用此类来创建步行路线图层。如不满足需求,也可以自己创建自定义的步行路线图层。
 * @since V2.1.0
 */
public class WalkRouteOverlay extends RouteOverlay {

    private PolylineOptions mPolylineOptions;

    private BitmapDescriptor walkStationDescriptor= null;

    private WalkPath walkPath;
    /**
     * 通过此构造函数创建步行路线图层。
     * @param context 当前activity。
     * @param amap 地图对象。
     * @param path 步行路线规划的一个方案。详见搜索服务模块的路径查询包 -WalkSte</a></strong>。
     * @param start 起点。详见搜索服务模块的核心基础包 -LatLonPoint
     * @param end 终点。详见搜索服务模块的核心基础包 -LatLonPoint
     * @since V2.1.0
     */
    public WalkRouteOverlay(Context context, AMap amap, WalkPath path,
            LatLonPoint start, LatLonPoint end) {
        super(context);
        this.mAMap = amap;
        this.walkPath = path;
        startPoint = AMapUtil.convertToLatLng(start);
        endPoint = AMapUtil.convertToLatLng(end);
    }
    /**
     * 添加步行路线到地图中。
     * @since V2.1.0
     */
    public void addToMap() {
        initPolylineOptions();
        try {
            List<WalkStep> walkPaths = walkPath.getSteps();
            mPolylineOptions.add(startPoint);
            for (int i = 0; i < walkPaths.size(); i++) {
                WalkStep walkStep = walkPaths.get(i);
                LatLng latLng = AMapUtil.convertToLatLng(walkStep
                        .getPolyline().get(0));
                addWalkStationMarkers(walkStep, latLng);
                addWalkPolyLines(walkStep);
            }
            mPolylineOptions.add(endPoint);
            addStartAndEndMarker();
            showPolyline();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    /**
     * 检查这一步的最后一点和下一步的起始点之间是否存在空隙
     */
    private void checkDistanceToNextStep(WalkStep walkStep,
            WalkStep walkStep1) {
        LatLonPoint lastPoint = getLastWalkPoint(walkStep);
        LatLonPoint nextFirstPoint = getFirstWalkPoint(walkStep1);
        if (!(lastPoint.equals(nextFirstPoint))) {
            addWalkPolyLine(lastPoint, nextFirstPoint);
        }
    }

    /**
     * @param walkStep
     * @return
     */
    private LatLonPoint getLastWalkPoint(WalkStep walkStep) {
        return walkStep.getPolyline().get(walkStep.getPolyline().size() - 1);
    }

    /**
     * @param walkStep
     * @return
     */
    private LatLonPoint getFirstWalkPoint(WalkStep walkStep) {
        return walkStep.getPolyline().get(0);
    }

    private void addWalkPolyLine(LatLonPoint pointFrom, LatLonPoint pointTo) {
        addWalkPolyLine(AMapUtil.convertToLatLng(pointFrom), AMapUtil.convertToLatLng(pointTo));
    }

    private void addWalkPolyLine(LatLng latLngFrom, LatLng latLngTo) {
        mPolylineOptions.add(latLngFrom, latLngTo);
    }

    /**
     * @param walkStep
     */
    private void addWalkPolyLines(WalkStep walkStep) {
        mPolylineOptions.addAll(AMapUtil.convertArrList(walkStep.getPolyline()));
    }

    /**
     * @param walkStep
     * @param position
     */
    private void addWalkStationMarkers(WalkStep walkStep, LatLng position) {
        addStationMarker(new MarkerOptions()
                .position(position)
                .title("\u65B9\u5411:" + walkStep.getAction()
                        + "\n\u9053\u8DEF:" + walkStep.getRoad())
                .snippet(walkStep.getInstruction()).visible(nodeIconVisible)
                .anchor(0.5f, 0.5f).icon(walkStationDescriptor));
    }

    /**
     * 初始化线段属性
     */
    private void initPolylineOptions() {
        if(walkStationDescriptor == null) {
            walkStationDescriptor = getWalkBitmapDescriptor();
        }
        mPolylineOptions = null;
        mPolylineOptions = new PolylineOptions();
        mPolylineOptions.color(getWalkColor()).width(getRouteWidth());
    }

    private void showPolyline() {
        addPolyLine(mPolylineOptions);
    }
}

RouteOverlay基类:

public class RouteOverlay {
    protected List<Marker> stationMarkers = new ArrayList<Marker>();
    protected List<Polyline> allPolyLines = new ArrayList<Polyline>();
    protected Marker startMarker;
    protected Marker endMarker;
    protected LatLng startPoint;
    protected LatLng endPoint;
    protected AMap mAMap;
    private Context mContext;
    private Bitmap startBit, endBit, busBit, walkBit, driveBit;
    protected boolean nodeIconVisible = true;

    public RouteOverlay(Context context) {
        mContext = context;
    }

    /**
     * 去掉BusRouteOverlay上所有的Marker。
     * @since V2.1.0
     */
    public void removeFromMap() {
        if (startMarker != null) {
            startMarker.remove();

        }
        if (endMarker != null) {
            endMarker.remove();
        }
        for (Marker marker : stationMarkers) {
            marker.remove();
        }
        for (Polyline line : allPolyLines) {
            line.remove();
        }
        destroyBit();
    }

    private void destroyBit() {
        if (startBit != null) {
            startBit.recycle();
            startBit = null;
        }
        if (endBit != null) {
            endBit.recycle();
            endBit = null;
        }
        if (busBit != null) {
            busBit.recycle();
            busBit = null;
        }
        if (walkBit != null) {
            walkBit.recycle();
            walkBit = null;
        }
        if (driveBit != null) {
            driveBit.recycle();
            driveBit = null;
        }
    }
    /**
     * 给起点Marker设置图标,并返回更换图标的图片。如不用默认图片,需要重写此方法。
     * @return 更换的Marker图片。
     * @since V2.1.0
     */
    protected BitmapDescriptor getStartBitmapDescriptor() {
        return BitmapDescriptorFactory.fromResource(R.mipmap.start);
    }
    /**
     * 给终点Marker设置图标,并返回更换图标的图片。如不用默认图片,需要重写此方法。
     * @return 更换的Marker图片。
     * @since V2.1.0
     */
    protected BitmapDescriptor getEndBitmapDescriptor() {
        return BitmapDescriptorFactory.fromResource(R.mipmap.end);
    }
    /**
     * 给公交Marker设置图标,并返回更换图标的图片。如不用默认图片,需要重写此方法。
     * @return 更换的Marker图片。
     * @since V2.1.0
     */
    protected BitmapDescriptor getBusBitmapDescriptor() {
        return BitmapDescriptorFactory.fromResource(R.mipmap.bus);
    }
    /**
     * 给步行Marker设置图标,并返回更换图标的图片。如不用默认图片,需要重写此方法。
     * @return 更换的Marker图片。
     * @since V2.1.0
     */
    protected BitmapDescriptor getWalkBitmapDescriptor() {
        return BitmapDescriptorFactory.fromResource(R.mipmap.man);
    }

    protected BitmapDescriptor getDriveBitmapDescriptor() {
        return BitmapDescriptorFactory.fromResource(R.mipmap.car);
    }

    protected void addStartAndEndMarker() {
        startMarker = mAMap.addMarker((new MarkerOptions())
                .position(startPoint).icon(getStartBitmapDescriptor())
                .title("\u8D77\u70B9"));
        // startMarker.showInfoWindow();

        endMarker = mAMap.addMarker((new MarkerOptions()).position(endPoint)
                .icon(getEndBitmapDescriptor()).title("\u7EC8\u70B9"));
        // mAMap.moveCamera(CameraUpdateFactory.newLatLngZoom(startPoint,
        // getShowRouteZoom()));
    }
    /**
     * 移动镜头到当前的视角。
     * @since V2.1.0
     */
    public void zoomToSpan() {
        if (startPoint != null) {
            if (mAMap == null)
                return;
            try {
                LatLngBounds bounds = getLatLngBounds();
                mAMap.animateCamera(CameraUpdateFactory
                        .newLatLngBounds(bounds, 50));
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }

    protected LatLngBounds getLatLngBounds() {
        LatLngBounds.Builder b = LatLngBounds.builder();
        b.include(new LatLng(startPoint.latitude, startPoint.longitude));
        b.include(new LatLng(endPoint.latitude, endPoint.longitude));
        return b.build();
    }
    /**
     * 路段节点图标控制显示接口。
     * @param visible true为显示节点图标,false为不显示。
     * @since V2.3.1
     */
    public void setNodeIconVisibility(boolean visible) {
        try {
            nodeIconVisible = visible;
            if (this.stationMarkers != null && this.stationMarkers.size() > 0) {
                for (int i = 0; i < this.stationMarkers.size(); i++) {
                    this.stationMarkers.get(i).setVisible(visible);
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    protected void addStationMarker(MarkerOptions options) {
        if(options == null) {
            return;
        }
        Marker marker = mAMap.addMarker(options);
        if(marker != null) {
            stationMarkers.add(marker);
        }

    }

    protected void addPolyLine(PolylineOptions options) {
        if(options == null) {
            return;
        }
        Polyline polyline = mAMap.addPolyline(options);
        if(polyline != null) {
            allPolyLines.add(polyline);
        }
    }

    protected float getRouteWidth() {
        return 18f;
    }

    protected int getWalkColor() {
        return Color.parseColor("#6db74d");
    }

    /**
     * 自定义路线颜色。
     * return 自定义路线颜色。
     * @since V2.2.1
     */
    protected int getBusColor() {
        return Color.parseColor("#537edc");
    }

    protected int getDriveColor() {
        return Color.parseColor("#537edc");
    }

    // protected int getShowRouteZoom() {
    // return 15;
    // }
}

工具类AMapUtil:

public class AMapUtil {

    /**
     * 把LatLonPoint对象转化为LatLon对象
     */
    public static LatLng convertToLatLng(LatLonPoint latLonPoint) {
        return new LatLng(latLonPoint.getLatitude(), latLonPoint.getLongitude());
    }

    /**
     * 把集合体的LatLonPoint转化为集合体的LatLng
     */
    public static ArrayList<LatLng> convertArrList(List<LatLonPoint> shapes) {
        ArrayList<LatLng> lineShapes = new ArrayList<>();
        for (LatLonPoint point : shapes) {
            LatLng latLngTemp = AMapUtil.convertToLatLng(point);
            lineShapes.add(latLngTemp);
        }
        return lineShapes;
    }
}

结尾:如上复制的话只有图片会报错了,找到Demo路径下的 src\main\res\drawable-xhdpi 资源目录,将对应图片复制进自己Demo即可

这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_34536167/article/details/79338243