Last active
March 21, 2020 21:47
-
-
Save ParryPatel021/60fe1266135e7f30e6438515619190c2 to your computer and use it in GitHub Desktop.
Method to decode polyline points in Java. Credit goes to https://jeffreysambells.com/2010/05/27/decoding-polylines-from-google-maps-direction-api-with-java
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
//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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you sir. This solution worked for me