Reputation: 1005
Am working on an Android project where I want to draw route between 2 point on Google Map. I have successfully drawn route between source and destination point. But I have 1 problem in that, i.e. Some times I want to draw a path between more than 2 point, That time the code that I have written is drawing route between first and the last position and leaving the mid point position. What I exactly want is my route should go through the mid point to the destination point. How can I achieve this?
Upvotes: 1
Views: 1292
Reputation: 2191
You can use PolylineOptions from GoogleMaps API Dcoumented Here and keep adding all the points which should be a part of your route. You can do something like this
ArrayList<LatLng> points;
PolylineOptions lineOptions = null;
// Traversing through all the routes
for (int i = 0; i < result.size(); i++) {
points = new ArrayList<>();
lineOptions = new PolylineOptions();
// Fetching i-th route
List<HashMap<String, String>> path = result.get(i);
// Fetching all the points in i-th route
for (int j = 0; j < path.size(); j++) {
HashMap<String, String> point = path.get(j);
double lat = Double.parseDouble(point.get("lat"));
double lng = Double.parseDouble(point.get("lng"));
LatLng position = new LatLng(lat, lng);
points.add(position);
}
// Adding all the points in the route to LineOptions
lineOptions.addAll(points);
lineOptions.width(10);
lineOptions.color(Color.RED);
}
// Drawing polyline in the Google Map for the i-th route
if(lineOptions != null) {
mMap.addPolyline(lineOptions);
}
Hope this solves your problem.
Upvotes: 0
Reputation: 3001
You can separately draw routes between three point. So, first draw the route from starting point to mid point then from mid point to end point. Then if you want add the required markers.
Upvotes: 0