polyline 1.0.2

  • Readme
  • Changelog
  • Example
  • Installing
  • new73

Pub

polyline.dart #

Polyline encoding is a lossy compression algorithm that allows you to store a series of coordinates as a single string.

Polyline is a dart port of Google's Polyline Algorithm explained here. Inspired by Mapbox's polyline.js. Compatible with Dart 2.

Installation #

Add polyline: ^1.0.2 to your pubspec.yaml. Then run:

pub get # flutter pub get

Usage #

A simple usage example:
Two named constructors are provided Polyline.Decode and Polyline.Encode.

Calling Polyline.Encode will compute the encodedString and set the passed in coordinates to decodedCoords on the instance of Polyline so that each instance of Polyline has access to the correct encoded string and subsequent decoded coordinates. Likewise, calling Polyline.Decode computes the list of coordinates from the encoded string and sets the passed in encoded string to the instance of Polyline.

Note that a precision of 5 is standard.

import 'package:polyline/polyline.dart';

void main() {
  Polyline polyline;

  /// List<List<double> coordinates;
  const coordinates = [
    [33.80119, -84.34788],
    [35.10566, -80.8762],
    [30.4526, -81.71116],
    [28.57888, -81.2717]
  ];
  const precision = 5;
  const encoded = 'mxhmEfeyaO}w}F_aeTrxk[nabDv}lJsytA';

  // Encode a list of coordinates with precision 5 to produce the encoded string
  polyline = Polyline.Encode(decodedCoords: coordinates, precision: 5);
  print('Encoded String: ${polyline.encodedString}, Coords: ${polyline.decodedCoords}');

  // Decode an encoded string to a list of coordinates
  polyline = Polyline.Decode(encodedString: encoded, precision: precision);
  print('Decoded Coords: ${polyline.decodedCoords}');
  print('String: ${polyline.encodedString}');

  // Calculate the distance of an encoded polyline, and decode the polyline
  polyline =  Polyline.Distance(encodedString: encoded,  unit: 'kilometers');
  // By calling  length the encodedString, decodedCoords, and distance variables
  // of the Polyline class are available
  print('Distance: ${polyline.distance.floor()}km , Encoded String: ${polyline.encodedString} Decoded Coords: ${polyline.decodedCoords}');

Documentation #

Api Documentation

Development for v1.0.3 #

Multiple flags for extended capabilities

  • [X] add haversine
  • [X] add haversine distance
  • [ ] Todo add merge polylines
  • [ ] Todo add merge multiple polylines
  • [ ] Todo add from geoJson

Features and bugs #

Please file feature requests and bugs at the issue tracker.

1.0.2 #

  • Distance constructor

1.0.1 #

  • Class constructors

1.0.0 #

  • Encode and Decode a polyline

example/polyline_example.dart

import 'package:polyline/polyline.dart';

void main() {
  Polyline polyline;

  /// List<List<double> coordinates;
  const coordinates = [
    [33.80119, -84.34788],
    [35.10566, -80.8762],
    [30.4526, -81.71116],
    [28.57888, -81.2717]
  ];
  const precision = 5;
  const encoded = 'mxhmEfeyaO}w}F_aeTrxk[nabDv}lJsytA';

  // Encode a list of coordinates with precision 5 to produce the encoded string
  polyline = Polyline.Encode(decodedCoords: coordinates, precision: 5);
  print('Encoded String: ${polyline.encodedString}, Coords: ${polyline.decodedCoords}');

  // Decode an encoded string to a list of coordinates
  polyline = Polyline.Decode(encodedString: encoded, precision: precision);
  print('Decoded Coords: ${polyline.decodedCoords}');
  print('String: ${polyline.encodedString}');

  // Calculate the distance of an encoded polyline, and decode the polyline
  polyline =  Polyline.Distance(encodedString: encoded,  unit: 'kilometers');
  // By calling  length the encodedString, decodedCoords, and distance variables
  // of the Polyline class are available
  print('Distance: ${polyline.distance.floor()}km , Encoded String: ${polyline.encodedString} Decoded Coords: ${polyline.decodedCoords}');
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  polyline: ^1.0.2

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:polyline/polyline.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
47
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
73
Learn more about scoring.

We analyzed this package on Jan 19, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.4

Health suggestions

Format lib/src/polyline_base.dart.

Run dartfmt to format lib/src/polyline_base.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.0 <3.0.0
Dev dependencies
pedantic ^1.0.0
test ^1.0.0