Skip to content

Instantly share code, notes, and snippets.

@JosLuna98
Last active October 26, 2020 16:36
Show Gist options
  • Save JosLuna98/32d680b215726b788874427ef842f29a to your computer and use it in GitHub Desktop.
Save JosLuna98/32d680b215726b788874427ef842f29a to your computer and use it in GitHub Desktop.
[Dart] Decoder for google encoded polyline
import 'model.dart';
void main() {
List<LatLng> decodeEncodedPolyline({@required String encoded}) {
List<LatLng> polyline = [];
int index = 0, len = encoded.length;
int lat = 0, lng = 0;
int decode(int positon) {
int shift = 0, result = 0;
do {
positon = encoded.codeUnitAt(index++) - 63;
result |= (positon & 0x1f) << shift;
shift += 5;
} while (positon >= 0x20);
return ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
}
while (index < len) {
int positon;
lat += decode(positon);
lng += decode(positon);
LatLng point = LatLng((lat / 1E5).toDouble(), (lng / 1E5).toDouble());
polyline.add(point);
}
return polyline;
}
List<LatLng> polyline = decodeEncodedPolyline(
encoded:
"qw{DnsdeN]}Ac@HsFt@eK~AqBeN}IrAkIrAcC`@cMrBoDl@kDj@Ec@OeAMq@So@AQMmBIwA]wDsA@Ag@PbF@zAs@F{@BRx@VbARx@RJiO|BsIrAmDj@{AyB}ByCm@w@qDyEs@y@y@k@q@So@Gu@A_AJs@Xk@\\]`@[l@Wn@IvAKlDUxJOfJMrF");
polyline.forEach((element) => print(element.toString()));
}
class LatLng {
LatLng(this.latitude, this.longitude)
: assert(latitude != null || longitude != null,
"latitude and longitude can't be null");
final double latitude;
final double longitude;
@override
String toString() {
return "$latitude,$longitude";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment