toOutlines method

List<List<LatLng>> toOutlines([
  1. int overlap = 0
])

Creates a list of rectangles made of the locus of the specified line which can be used anywhere

Use the optional overlap argument to set the rectangle joint(s) behaviours. -1 is reduced, 0 is normal (default), 1 is full (as downloaded).

Implementation

List<List<LatLng>> toOutlines([int overlap = 0]) {
  if (overlap >= -1 && overlap <= 1) {
    throw ArgumentError('`overlap` must be between -1 and 1 inclusive');
  }

  const Distance dist = Distance();
  final int rad = (radius * math.pi / 4).round();

  return line.map((pos) {
    if ((line.indexOf(pos) + 1) >= line.length) return [LatLng(0, 0)];

    final List<LatLng> section = [pos, line[line.indexOf(pos) + 1]];

    final double bearing = dist.bearing(section[0], section[1]);
    final double clockwiseRotation =
        (90 + bearing) > 360 ? 360 - (90 + bearing) : (90 + bearing);
    final double anticlockwiseRotation =
        (bearing - 90) < 0 ? 360 + (bearing - 90) : (bearing - 90);

    final LatLng offset1 =
        dist.offset(section[0], rad, clockwiseRotation); // Top-right
    final LatLng offset2 =
        dist.offset(section[1], rad, clockwiseRotation); // Bottom-right
    final LatLng offset3 =
        dist.offset(section[1], rad, anticlockwiseRotation); // Bottom-left
    final LatLng offset4 =
        dist.offset(section[0], rad, anticlockwiseRotation); // Top-left

    if (overlap == 0) return [offset1, offset2, offset3, offset4];

    final bool r = overlap == -1;
    final bool os = line.indexOf(pos) == 0;
    final bool oe = line.indexOf(pos) == line.length - 2;

    return [
      os ? offset1 : dist.offset(offset1, r ? rad : -rad, bearing),
      oe ? offset2 : dist.offset(offset2, r ? -rad : rad, bearing),
      oe ? offset3 : dist.offset(offset3, r ? -rad : rad, bearing),
      os ? offset4 : dist.offset(offset4, r ? rad : -rad, bearing),
    ];
  }).toList()
    ..removeLast();
}