Skip to content

Instantly share code, notes, and snippets.

@KiwiArch
Created October 6, 2020 21:09
Show Gist options
  • Save KiwiArch/e94ad8cad9fdfad3e722ee168ab58c5f to your computer and use it in GitHub Desktop.
Save KiwiArch/e94ad8cad9fdfad3e722ee168ab58c5f to your computer and use it in GitHub Desktop.
Polyline Decode incorrect results using Flutter Web
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class GeoPoint {
GeoPoint(this.latitude, this.longitude);
final double latitude;
final double longitude;
@override
toString() {
return '$latitude, $longitude';
}
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Sandbox',
theme: ThemeData(
backgroundColor: Colors.white,
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: Container(child: Builder(builder: (context) {
final geoString =
r"dg_{Foovi`@WsBz@w@lj@hW`FvCfKMRx@@KAJSy@gKL}CqByfAgg@?uCRCOc@Nb@~[aDbHaE|FpChCsFiCrF}FqCcH`Eq`@rEiOsGiC|BsPkJkKl@tGJnAe@DJjA^nAzG\p@Z[x@jA`@bB|@u@Z|@fE}D~YnMZqA";
final results = decodeEncodedPolyline(geoString);
return Column(
children: [
//Text(geoString),
Expanded(
child: ListView.builder(
itemCount: results.length, itemBuilder: (context, index) => Text(results[index].toString(),style: TextStyle(fontSize: 14, color: Colors.black),))),
],
);
})),
);
}
static List<GeoPoint> decodeEncodedPolyline(String encoded) {
print('decode polyline: $encoded');
List<GeoPoint> points = [];
int index = 0, len = encoded.length;
int lat = 0, lng = 0;
while (index < len) {
int b, shift = 0, result = 0;
do {
b = encoded.codeUnitAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = encoded.codeUnitAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;
GeoPoint p = new GeoPoint((lat / 1E5).toDouble(), (lng / 1E5).toDouble());
points.add(p);
}
return points;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment