centroid property

GeoPoint get centroid

Returns the centroid (geometric center) of this polygon.

Uses the arithmetic mean of all vertex coordinates.

Example:

final polygon = GeoPolygon(points: [...]);
final center = polygon.centroid;

Implementation

GeoPoint get centroid {
  if (points.isEmpty) {
    throw StateError('Cannot compute centroid of empty polygon');
  }

  double totalLat = 0;
  double totalLng = 0;

  for (final point in points) {
    totalLat += point.latitude;
    totalLng += point.longitude;
  }

  return GeoPoint(
    latitude: totalLat / points.length,
    longitude: totalLng / points.length,
  );
}