call method

  1. @override
LatLng call(
  1. Polygon<Object> polygon
)
override

Given a polygon (and its points), calculate a single position at which the center of the label should be placed

Polygon.points is not guaranteed to be non-empty. If empty, this may throw.

Implementation

@override
LatLng call(Polygon polygon) {
  if (polygon.points.isEmpty) {
    throw ArgumentError('Polygon must contain at least one point');
  }

  const halfWorld = 180;
  int count = 0;
  double sum = 0;
  late double lastLng;
  for (final LatLng point in polygon.points) {
    double lng = point.longitude;
    count++;
    if (count > 1) {
      if (lng - lastLng > halfWorld) {
        lng -= 2 * halfWorld;
      } else if (lng - lastLng < -halfWorld) {
        lng += 2 * halfWorld;
      }
    }
    lastLng = lng;
    sum += lastLng;
  }
  return LatLng(polygon.points.map((e) => e.latitude).average, sum / count);
}