First of all, you should use the Google V2 Maps API instead of the old legacy V1 API
In action, create links in Google Map and Polyline:
public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback{ private GoogleMap mMap; Polyline line;
First define your waypoint list:
List<LatLng> latLngWaypointList = new ArrayList<>();
Get your route, draw a polyline for the route, and then draw waypoint markers:
class GetDirectionsAsync extends AsyncTask<LatLng, Void, List<LatLng>> { @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected List<LatLng> doInBackground(LatLng... params) { List<LatLng> route = new ArrayList<>(); //populate route...... return route; } @Override protected void onPostExecute(List<LatLng> route) { if (route == null) return; if (line != null){ line.remove(); } PolylineOptions options = new PolylineOptions().width(5).color(Color.MAGENTA).geodesic(true); for (int i = 0; i < pointsList.size(); i++) { LatLng point = route.get(i); //If this point is a waypoint, add it to the list if (isWaypoint(i)) { latLngWaypointList.add(point); } options.add(point); } //draw the route: line = mMap.addPolyline(options); //draw waypoint markers: for (LatLng waypoint : latLngWaypointList) { mMap.addMarker(new MarkerOptions().position(new LatLng(waypoint.latitude, waypoint.longitude)) .title("Waypoint").icon(BitmapDescriptorFactory .defaultMarker(BitmapDescriptorFactory.HUE_VIOLET))); } } }
Here is just an implementation of placeholder isWaypoint ():
public boolean isWaypoint(int i) {
The result for a route from the west coast to the east coast, about 2,500 miles:

The result of a smaller route:

Note that in this example, I also use Google Directions Api to make the route snapped to roads. For a complete example of how to use the Google Directions api, see here: fooobar.com/questions/882026 / ...
Daniel Nugent
source share