encodePolyline function

String encodePolyline(
  1. List<List<num>> coordinates, {
  2. int accuracyExponent = 5,
})

Encodes List<List<num>> of coordinates into a String via Encoded Polyline Algorithm Format

For mode detailed info about encoding refer to encodePoint.

Implementation

String encodePolyline(List<List<num>> coordinates, {int accuracyExponent = 5}) {
  assert(() {
    if (accuracyExponent < 1) {
      throw ArgumentError.value(accuracyExponent, 'accuracyExponent',
          'Location accuracy exponent cannot be less than 1');
    }

    if (accuracyExponent > 9) {
      throw ArgumentError.value(
          accuracyExponent,
          'accuracyExponent',
          'Location accuracy exponent cannot be greater than 9.\n\n'
              'For more info why (cannot be greater than 9), refer to table of what each digit '
              'in a decimal degree signifies:\n'
              ' * The sign tells us whether we are north or south, east or west on the globe.\n'
              ' * A nonzero hundreds digit tells us we\'re using longitude, not latitude!\n'
              ' * The tens digit gives a position to about 1,000 kilometers. It gives us useful '
              'information about what continent or ocean we are on.\n'
              ' * The units digit (one decimal degree) gives a position up to 111 kilometers (60 '
              'nautical miles, about 69 miles). It can tell us roughly what large state or country '
              'we are in.\n'
              ' * The first decimal place is worth up to 11.1 km: it can distinguish the position '
              'of one large city from a neighboring large city.\n'
              ' * The second decimal place is worth up to 1.1 km: it can separate one village from '
              'the next.\n'
              ' * The third decimal place is worth up to 110 m: it can identify a large agricultural '
              'field or institutional campus.\n'
              ' * The fourth decimal place is worth up to 11 m: it can identify a parcel of land. It '
              'is comparable to the typical accuracy of an uncorrected GPS unit with no interference.\n'
              ' * The fifth decimal place is worth up to 1.1 m: it distinguish trees from each other. '
              'Accuracy to this level with commercial GPS units can only be achieved with differential '
              'correction.\n'
              ' * The sixth decimal place is worth up to 0.11 m: you can use this for laying out '
              'structures in detail, for designing landscapes, building roads. It should be more '
              'than good enough for tracking movements of glaciers and rivers. This can be achieved '
              'by taking painstaking measures with GPS, such as differentially corrected GPS.\n'
              ' * The seventh decimal place is worth up to 11 mm: this is good for much surveying '
              'and is near the limit of what GPS-based techniques can achieve.\n'
              ' * The eighth decimal place is worth up to 1.1 mm: this is good for charting motions '
              'of tectonic plates and movements of volcanoes. Permanent, corrected, constantly-running '
              'GPS base stations might be able to achieve this level of accuracy.\n'
              ' * The ninth decimal place is worth up to 110 microns: we are getting into the range '
              'of microscopy. For almost any conceivable application with earth positions, this is '
              'overkill and will be more precise than the accuracy of any surveying device.\n'
              ' * Ten or more decimal places indicates a computer or calculator was used and that no '
              'attention was paid to the fact that the extra decimals are useless. Be careful, because '
              'unless you are the one reading these numbers off the device, this can indicate low '
              'quality processing!');
    }

    return true;
  }());

  if (coordinates.isEmpty) return '';

  String polyline =
      encodePoint(coordinates[0][0], accuracyExponent: accuracyExponent) +
          encodePoint(coordinates[0][1], accuracyExponent: accuracyExponent);

  for (var i = 1; i < coordinates.length; i++) {
    polyline += encodePoint(coordinates[i][0],
        previous: coordinates[i - 1][0], accuracyExponent: accuracyExponent);
    polyline += encodePoint(coordinates[i][1],
        previous: coordinates[i - 1][1], accuracyExponent: accuracyExponent);
  }

  return polyline;
}