Skip to content

Instantly share code, notes, and snippets.

@Ankit-Slnk
Created March 15, 2023 11:38
Show Gist options
  • Save Ankit-Slnk/a2099d0e980de87648ea58b557fe3122 to your computer and use it in GitHub Desktop.
Save Ankit-Slnk/a2099d0e980de87648ea58b557fe3122 to your computer and use it in GitHub Desktop.
Encoded polyline to google polyline
static List<LatLng> decodeEncodedPolyline(String encoded) {
List<LatLng> poly = [];
int index = 0, len = encoded.length;
int lat = 0, lng = 0;
while (index < len) {
int b, shift = 0, result = 0;
do {
b = encoded.codeUnitAt(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.codeUnitAt(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(
(lat / 1E5).toDouble(),
(lng / 1E5).toDouble(),
);
poly.add(p);
}
return poly;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment