androidgoogle-mapsgoogle-maps-api-3android-mapsgoogle-directions-api

Drawing a direction on Google maps is not continuous


I'm using these settings to create a direction from a location to another:

PolylineOptions polylineOptions = new PolylineOptions();
polylineOptions.width(15);
polylineOptions.color(colorPrimary);
mMap.addPolyline(polylineOptions.addAll(points));

And on the map, it looks like this:

https://ibb.co/D13xc0T

How to draw a continuous line from one point to another without those interruptions?

This is the code that gets the points:

if (direction != null) {
    List<Route> routes = direction.routes;
    Route route = routes.get(0);
    List<Leg> legs = route.legs;
    Leg leg = legs.get(0);
    List<Step> steps = leg.steps;
    for (Step step : steps) {
        String point = step.polyline.points;
        List<LatLng> points = PolyUtil.decode(point);
    }
}

Solution

  • You add many PolylineOptions to map. They are not connected to each over and so you get this interruptions. Try to add all decoded paths to one common array.

    Your edited code:

    ArrayList<LatLng> points = new ArrayList<LatLng>();
    for (Step step : steps) {
            String point = step.polyline.points;
            points.addAll(PolyUtil.decode(point));
        }
    
    PolylineOptions polylineOptions = new PolylineOptions();
    polylineOptions.width(15);
    polylineOptions.color(colorPrimary);
    mMap.addPolyline(polylineOptions.addAll(points));