1

我有一个 GeoJSON 数据,它给了我所有的点(坐标)。使用这些点,我可以使用以下代码在地图上绘制折线。

mapboxMap.addPolyline(new PolylineOptions()
                            .addAll(mapMatchedPoints)
                            .color(Color.GREEN)
                            .alpha(0.65f)
                            .width(4));

然后我想用这个导航地图上的所有点

 NavigationRoute.Builder navigationRoute = NavigationRoute.builder()
            .accessToken(Mapbox.getAccessToken());

    navigationRoute.origin(origin);
    navigationRoute.destination(destination);

这里需要起点和终点。监听其回调方法会在以下代码中提供路由

navigationRoute.build().getRoute(new Callback<DirectionsResponse>() {
        @Override
        public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
            if (response.body() == null) {
                Log.e(TAG, "No routes found, make sure you set the right user and access token.");
                return;
            }

            // Print some info about the route
            route = response.body().getRoutes().get(0);
            Log.d(TAG, "Distance: " + route.getDistance());

            // Draw the route on the map
            drawRoute(route, origin, destination);
        }

        @Override
        public void onFailure(Call<DirectionsResponse> call, Throwable t) {
            Log.e(TAG, "Error: " + t.getMessage());
        }
    });

drawroute 是用于绘制导航路线的方法

private void drawRoute(DirectionsRoute route, Position origin, Position destination) {
    // Convert LineString coordinates into LatLng[]
    LineString lineString = LineString.fromPolyline(route.getGeometry(), Constants.PRECISION_6);
    List<Position> coordinates = lineString.getCoordinates();
    LatLng[] points = new LatLng[coordinates.size()];
    for (int i = 0; i < coordinates.size(); i++) {
        points[i] = new LatLng(
                coordinates.get(i).getLatitude(),
                coordinates.get(i).getLongitude());
    }

    // Draw Points on MapView
    mapboxMap.addPolyline(new PolylineOptions()
            .add(points)
            .color(Color.parseColor("#3887be"))
            .width(5));
    mapboxMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(origin.getLatitude(), origin.getLongitude()), 18));

    mapboxMap.addMarker(new MarkerOptions().position(new LatLng(origin.getLatitude(), origin.getLongitude())).setTitle("Origin"));
    mapboxMap.addMarker(new MarkerOptions().position(new LatLng(destination.getLatitude(), destination.getLongitude())).setTitle("Destination"));
}

但它并没有贯穿所有要点。

它只是从起点到终点,避免它们之间的中间点。

如此屏幕截图所示,它仅显示从起点到终点。

在此处输入图像描述

我希望它显示通过绿色标记折线的路线,并通过它导航。

我猜它正在计算点和画线之间的最短路径。

4

0 回答 0