Skip to content

Instantly share code, notes, and snippets.

@mitchtabian
Last active September 26, 2022 18:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mitchtabian/01b629e3df2f7b52ed1733d6effebbf7 to your computer and use it in GitHub Desktop.
Save mitchtabian/01b629e3df2f7b52ed1733d6effebbf7 to your computer and use it in GitHub Desktop.
Adding Polylines to a Google Map
private void addPolylinesToMap(final DirectionsResult result){
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Log.d(TAG, "run: result routes: " + result.routes.length);
for(DirectionsRoute route: result.routes){
Log.d(TAG, "run: leg: " + route.legs[0].toString());
List<com.google.maps.model.LatLng> decodedPath = PolylineEncoding.decode(route.overviewPolyline.getEncodedPath());
List<LatLng> newDecodedPath = new ArrayList<>();
// This loops through all the LatLng coordinates of ONE polyline.
for(com.google.maps.model.LatLng latLng: decodedPath){
// Log.d(TAG, "run: latlng: " + latLng.toString());
newDecodedPath.add(new LatLng(
latLng.lat,
latLng.lng
));
}
Polyline polyline = mGoogleMap.addPolyline(new PolylineOptions().addAll(newDecodedPath));
polyline.setColor(ContextCompat.getColor(getActivity(), R.color.darkGrey));
polyline.setClickable(true);
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment