Tim Nuwin Tim Nuwin - 1 year ago 154
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()

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

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

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(

// Draw Points on MapView
map.addPolyline(new PolylineOptions()

enter image description here

Answer Source

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()
  .setOverview(DirectionsCriteria.OVERVIEW_FULL) // This line needs to be added.
  .setAccessToken(<access token>)

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download