decodeGeometry method

List<LngLat> decodeGeometry({
  1. int precision = 5,
})

Implementation

List<LngLat> decodeGeometry({int precision = 5}) {
  final List<LngLat> coordinates = [];

  var index = 0,
      lat = 0,
      lng = 0,
      shift = 0,
      result = 0,
      factor = math.pow(10, precision);

  int? latitudeChange, longitudeChange, byte;

  // Coordinates have variable length when encoded, so just keep
  // track of whether we've hit the end of the string. In each
  // loop iteration, a single coordinate is decoded.
  while (index < length) {
    // Reset shift, result, and byte
    byte = null;
    shift = 0;
    result = 0;

    do {
      byte = codeUnitAt(index++) - 63;
      result |= ((Int32(byte) & Int32(0x1f)) << shift).toInt();
      shift += 5;
    } while (byte >= 0x20);

    latitudeChange =
        ((result & 1) != 0 ? ~(Int32(result) >> 1) : (Int32(result) >> 1))
            .toInt();

    shift = result = 0;

    do {
      byte = codeUnitAt(index++) - 63;
      result |= ((Int32(byte) & Int32(0x1f)) << shift).toInt();
      shift += 5;
    } while (byte >= 0x20);

    longitudeChange =
        ((result & 1) != 0 ? ~(Int32(result) >> 1) : (Int32(result) >> 1))
            .toInt();

    lat += latitudeChange;
    lng += longitudeChange;

    coordinates.add(LngLat(lat: lat / factor, lng: lng / factor));
  }

  return coordinates;
}