Tim Nuwin Tim Nuwin - 2 months ago 27
Android Question

Route overlay cuts through roads - Mapbox

This code that I copied from https://www.mapbox.com/android-sdk/examples/directions/ it doesn't display the overlay on the road, it'll cut through streets even though I specified the criteria to be 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));
}


enter image description here

Answer

Correct Answer: Thanks for providing the points, I didn't realize what was happening until I saw the entire route. You'll need to setOverview to full. This is done when building the directions request, for example:

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();

Previous troubleshooting: Which version of Mapobx Android Services are you using? Could you edit your question with the entire class or link to it on GIthub. I'm not seeing any issues looking at that code. For troubleshooting on your end, I'd recommend changing OSRM_PRECISION_V5 and check the size of the points array to see where things go wrong.

I'll edit this answer once additional information's provided.

EDIT: No need to change the encoding constant if you are using Mapbox directions V5. Instead can you make sure you importing all the correct classes, work off this example in the demo app. If you are still having issues, like mentioned below, please post any additional code you can provide, currently the snippet in your question is pretty much identical to the one found on our website.