Skip to content

Instantly share code, notes, and snippets.

@hleinone
Created June 29, 2022 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hleinone/c27af7e33ebfd2333d67bfaeaa97dc37 to your computer and use it in GitHub Desktop.
Save hleinone/c27af7e33ebfd2333d67bfaeaa97dc37 to your computer and use it in GitHub Desktop.
Google Maps for Flutter Polyline Decoding
import 'package:google_maps_flutter/google_maps_flutter.dart';
List<LatLng> polylineDecode(String polyline) {
final codeUnits = polyline.codeUnits;
final len = codeUnits.length;
// For speed we preallocate to an upper bound on the final length, then
// truncate the array before returning.
final path = <LatLng>[];
var index = 0;
var lat = 0;
var lng = 0;
while (index < len) {
var result = 1;
var shift = 0;
int b;
do {
b = codeUnits[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 = codeUnits[index++] - 63 - 1;
result += b << shift;
shift += 5;
} while (b >= 0x1f);
lng += (result & 1 != 0) ? ~(result >> 1) : result >> 1;
path.add(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