center property

GeoLatLng center

Calculates the center of a collection of geo coordinates

The function rounds the result to 6 decimals

Implementation

GeoLatLng get center {
    GValidate.notEmpty(coordinates,"Coordinates must not be empty!");

    double X = 0.0;
    double Y = 0.0;
    double 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 int 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)));
}