Created
June 29, 2022 21:25
-
-
Save hleinone/c27af7e33ebfd2333d67bfaeaa97dc37 to your computer and use it in GitHub Desktop.
Google Maps for Flutter Polyline Decoding
This file contains hidden or 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
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