Last active
October 26, 2020 16:36
-
-
Save JosLuna98/32d680b215726b788874427ef842f29a to your computer and use it in GitHub Desktop.
[Dart] Decoder for google encoded polyline
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 '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