Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ParryPatel021
Last active March 21, 2020 21:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ParryPatel021/60fe1266135e7f30e6438515619190c2 to your computer and use it in GitHub Desktop.
Save ParryPatel021/60fe1266135e7f30e6438515619190c2 to your computer and use it in GitHub Desktop.
//Calling decodePoly
String polyline = (String)((JSONObject)((JSONObject)jSteps.get(k)).get("polyline")).get("points");
List<LatLng> list = decodePoly(polyline);
/*
where "polyline" & "points" are JSON key return from GoogleMap URl.
*/
// deocdePoly implementation
private List<LatLng> decodePoly(String encoded) {
List<LatLng> poly = new ArrayList<>();
int index = 0, len = encoded.length();
int lat = 0, lng = 0;
while (index < len) {
int b, shift = 0, result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;
LatLng p = new LatLng((((double) lat / 1E5)),
(((double) lng / 1E5)));
poly.add(p);
}
return poly;
}
@molebogenggiven
Copy link

Thank you sir. This solution worked for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment