center property

LatLng center

Calculates the center of a collection of geo coordinates

The function rounds the result to 6 decimals

Implementation

LatLng get center {
  if (coordinates.isEmpty) {
    throw AssertionError('Coordinates must not be empty!');
  }

  var X = 0.0;
  var Y = 0.0;
  var Z = 0.0;

  double lat, lon, hyp;

  coordinates.forEach((final T coordinate) {
    lat = coordinate.latitudeInRad;
    lon = coordinate.longitudeInRad;

    X += math.cos(lat) * math.cos(lon);
    Y += math.cos(lat) * math.sin(lon);
    Z += math.sin(lat);
  });

  final nrOfCoordinates = coordinates.length;
  X = X / nrOfCoordinates;
  Y = Y / nrOfCoordinates;
  Z = Z / nrOfCoordinates;

  lon = math.atan2(Y, X);
  hyp = math.sqrt(X * X + Y * Y);
  lat = math.atan2(Z, hyp);

  return _latLngFactory(round(radianToDeg(lat)), round(radianToDeg(lon)));
}