Skip to content

Instantly share code, notes, and snippets.

@preetjdp
Created November 13, 2019 16:53
Show Gist options
  • Select an option

  • Save preetjdp/caa6d7a3b5f22d026dd0503e1a219071 to your computer and use it in GitHub Desktop.

Select an option

Save preetjdp/caa6d7a3b5f22d026dd0503e1a219071 to your computer and use it in GitHub Desktop.
This is the code required to decode the (encoded)polyline into a List of LatLng
import 'package:google_maps_flutter/google_maps_flutter.dart';
List<LatLng> decodePolyline(final String encodedPath) {
int len = encodedPath.length;
// For speed we preallocate to an upper bound on the final length, then
// truncate the array before returning.
final List<LatLng> path = [];
int index = 0;
int lat = 0;
int lng = 0;
while (index < len) {
int result = 1;
int shift = 0;
int b;
do {
b = encodedPath.codeUnitAt(index++) - 63 - 1;
result += b << shift;
shift += 5;
} while (b >= 0x1f);
lat += (result & 1) != 0 ? ~(result >> 1) : (result >> 1);
result = 1;
shift = 0;
do {
b = encodedPath.codeUnitAt(index++) - 63 - 1;
result += b << shift;
shift += 5;
} while (b >= 0x1f);
lng += (result & 1) != 0 ? ~(result >> 1) : (result >> 1);
path.add(new LatLng(lat * 1e-5, lng * 1e-5));
}
return path;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment