Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add polyline encoding and decoding #91

Closed
RaimundWege opened this issue Aug 27, 2018 · 9 comments
Closed

Add polyline encoding and decoding #91

RaimundWege opened this issue Aug 27, 2018 · 9 comments

Comments

@RaimundWege
Copy link
Contributor

As a developer I want to encode and decode polylines.

The following code can be used for decoding:

List<LatLng> decodePolyline(String encoded) {
  List<LatLng> points = new List<LatLng>();
  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;
    LatLng p = new LatLng(lat / 1E5, lng / 1E5);
    points.add(p);
  }
  return points;
}
@johnpryan
Copy link
Collaborator

johnpryan commented Aug 27, 2018

PolylineLayer takes a List<LatLng> already. Why does this function need to be included in the flutter_map project?

@RaimundWege
Copy link
Contributor Author

RaimundWege commented Aug 28, 2018

Eventually I miss something but many REST APIs return a polyline as an encoded String (like `~oia@ source: https://developers.google.com/maps/documentation/utilities/polylinealgorithm) and as a developer I want to write something like

Polyline polyline = new Polyline(encodedPoints: "`~oia@", color: Colors.blue);

or

print(polyline.encode()); // -> `~oia@

@johnpryan
Copy link
Collaborator

Is there a reason you can't build this as a separate package?

@RaimundWege
Copy link
Contributor Author

RaimundWege commented Aug 30, 2018

No. So I (or someone else) should publish this as a separate package? If so we can close this issue.

@johnpryan
Copy link
Collaborator

Yes I think that makes sense in this case, thanks!

@bijoya-banik
Copy link

Thank u, it works nicely

@aloenobilis
Copy link

Hi,
@RaimundWege @johnpryan recently published polyline.dart - https://github.com/sashvoncurtis/polyline.dart

Using flutter map I then use polyline.dart and decode the encoded string into a list of decoded coordinates.

1 similar comment
@aloenobilis
Copy link

Hi,
@RaimundWege @johnpryan recently published polyline.dart - https://github.com/sashvoncurtis/polyline.dart

Using flutter map I then use polyline.dart and decode the encoded string into a list of decoded coordinates.

@johnpryan
Copy link
Collaborator

@sashvoncurtis very cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants