Last active
September 26, 2022 18:24
-
-
Save mitchtabian/01b629e3df2f7b52ed1733d6effebbf7 to your computer and use it in GitHub Desktop.
Adding Polylines to a Google Map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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