路线叠加层穿过道路 - Mapbox

Route overlay cuts through roads - Mapbox

我从 https://www.mapbox.com/android-sdk/examples/directions/ 复制的代码它不显示道路上的叠加层,它会穿过街道,即使我将条件指定为 PROFILE_DRIVING:

private void getRoute(Position origin, Position destination) throws ServicesException {

        MapboxDirections client = new MapboxDirections.Builder()
                .setOrigin(origin)
                .setDestination(destination)
                .setProfile(DirectionsCriteria.PROFILE_DRIVING)
                .setAccessToken(CommonResource.MAPBOX_ACCESS_TOKEN)
                .build();

        client.enqueueCall(new retrofit2.Callback<DirectionsResponse>() {
            @Override
            public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
                // You can get the generic HTTP info about the response
                if (response.body() == null) {
                    return;
                }

                currentRoute = response.body().getRoutes().get(0);
                drawRoute(currentRoute);
            }

            @Override
            public void onFailure(Call<DirectionsResponse> call, Throwable t) {                    
                Toast.makeText(MainActivity.this, "Error: " + t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void drawRoute(DirectionsRoute route) {
        // Convert LineString coordinates into LatLng[]
        LineString lineString = LineString.fromPolyline(route.getGeometry(), Constants.OSRM_PRECISION_V5);
        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
        map.addPolyline(new PolylineOptions()
                .add(points)
                .color(Color.parseColor("#009688"))
                .width(5));
    }

正确答案:感谢您提供积分,我直到看到整个路线才意识到发生了什么。您需要将 setOverview 设置为完整。这是在构建路线请求时完成的,例如:

MapboxDirections client = new MapboxDirections.Builder()
  .setOrigin(origin)
  .setDestination(destination)
  .setProfile(DirectionsCriteria.PROFILE_CYCLING)
  .setOverview(DirectionsCriteria.OVERVIEW_FULL) // This line needs to be added.
  .setAccessToken(<access token>)
  .build();

之前的问题排查: 您使用的是哪个版本的 Mapobx Android 服务?您能否在 GIthub 上用整个 class 或 link 编辑您的问题。我没有看到查看该代码的任何问题。为了解决您的问题,我建议更改 OSRM_PRECISION_V5 并检查点数组的大小以查看哪里出了问题。

提供更多信息后,我将编辑此答案。

编辑:如果您使用的是 Mapbox directions V5,则无需更改编码常量。相反,您能否确保导入所有正确的 classes,完成 this example in the demo app。如果您仍有问题,如下所述,请 post 您可以提供任何其他代码,目前您问题中的代码段与我们网站上的代码段几乎相同。