Android 开发之高德地图绘制路线

under 高德地图  Android  tag     Published on July 27th , 2021 at 12:12 pm

获取路线数据
绘制线
结合搜索,在搜索列表中点击想去的位置

获取路线数据

  • 添加全局变量
private RouteSearch routeSearch;
private RouteSearch.FromAndTo fromAndTo;
private LatLonPoint fromPoint; // 起点(本篇起点为当前位置)
  • onLocationChanged 中获取起点,赋值给全局变量
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
    if (aMapLocation != null) {
        if (aMapLocation.getErrorCode() == 0) {
            // ...
            fromPoint = new LatLonPoint(aMapLocation.getLatitude(), aMapLocation.getLongitude());
        }
    }
}
  • setToPoint() 设置终点
public void setToPoint(LatLonPoint toPoint) {
    routeSearch = new RouteSearch(context);
    // 需要重写方法
    routeSearch.setRouteSearchListener(this);
    fromAndTo = new RouteSearch.FromAndTo(getFromPoint(), toPoint);
    RouteSearch.WalkRouteQuery query = new RouteSearch.WalkRouteQuery(fromAndTo, RouteSearch.WALK_MULTI_PATH);
    routeSearch.calculateWalkRouteAsyn(query);
    }
  • 点击 ListView 的 item 后调用
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
     @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        lv.setVisibility(View.GONE);
        aMap.moveCamera(CameraUpdateFactory.zoomTo(12));
        setToPoint(new LatLonPoint(data.get(position).getPoint().getLatitude(), data.get(position).getPoint().getLongitude()));
    }
});

绘制线

setRouteSearchListener

重写方法(我这里写的是步行路线)

返回的信息中您可以获得路段的距离、步行的预计时间、步行路段的坐标点、步行路段的道路名称、导航主要操作等信息。

i 为返回结果成功或者失败的响应码。等于1000为成功,其他为失败。

    @Override
    public void onBusRouteSearched(BusRouteResult busRouteResult, int i) {

    }

    @Override
    public void onDriveRouteSearched(DriveRouteResult driveRouteResult, int i) {

    }

    @Override
    public void onWalkRouteSearched(WalkRouteResult walkRouteResult, int i) {
        List<WalkPath> list = walkRouteResult.getPaths();
        for (WalkPath path : list) {
            List<LatLonPoint> list_latlonpoint = path.getPolyline();
            List<LatLng> lineList = new ArrayList<>();
            for (LatLonPoint latLonPoint : list_latlonpoint) {
                lineList.add(new LatLng(latLonPoint.getLatitude(), latLonPoint.getLongitude()));
            }
            //绘制路线
            aMap.clear();
            aMap.addPolyline(new PolylineOptions().addAll(lineList).width(10).color(Color.argb(255, 1, 1, 1)));
        }
    }

    @Override
    public void onRideRouteSearched(RideRouteResult rideRouteResult, int i) {

    }

本文由 surface 创作,采用 知识共享署名4.0 国际许可协议进行许可,转载前请务必署名
  文章最后更新时间为:August 11th , 2021 at 10:32 am
分享到:Twitter  Weibo  Facebook