Reputation: 175
I have an application that retrieves a list of routes via the com.google.maps.DirectionsApi class and returns a route in an object of type com.google.maps.model.DirectionsResult, how do I display the route contained in this object in a Google Maps Activity?
Upvotes: 1
Views: 784
Reputation: 32138
I understand you are using the Java Client for Google Maps Services.
You should loop through the structure of com.google.maps.model.DirectionsResult to get all LatLng and use Polyline to draw the route on the map.
The com.google.maps.model.DirectionsResult contains an array of routes (DirectionsRoute). The com.google.maps.model.DirectionsRoute contains an array of legs (DirectionsLeg). The com.google.maps.model.DirectionsLeg contains an array of steps (DirectionsStep). The com.google.maps.model.DirectionsStep contains encoded polyline. You can get a list of LatLng from each encoded polyline and use it to contruct the Polyline object.
The code snippet might be something like
for (int i=0; i<result.routes.length; i++) {
DirectionsRoute route = result.routes[i];
PolylineOptions polyOptions = new PolylineOptions();
for (int j=0; j<route.legs.length; j++) {
DirectionsLeg leg = route.legs[j];
for (int k=0; k<leg.steps.length; k++) {
DirectionsStep step = leg.steps[k];
EncodedPolyline encpoly = step.polyline;
List<LatLng> points = encpoly.decodePath();
polyOptions.addAll(points);
}
}
Polyline p = googleMap.addPolyline(polyOptions);
}
Upvotes: 1