center property

LatLng center

Obtain coordinates of the bounds center

Implementation

LatLng get center {
  // https://stackoverflow.com/a/4656937
  // http://www.movable-type.co.uk/scripts/latlong.html
  // coord 1: southWest
  // coord 2: northEast
  // phi: lat
  // lambda: lng

  final phi1 = south * degrees2Radians;
  final lambda1 = west * degrees2Radians;
  final phi2 = north * degrees2Radians;

  // delta lambda = lambda2-lambda1
  final dLambda = degrees2Radians * (east - west);

  final bx = cos(phi2) * cos(dLambda);
  final by = cos(phi2) * sin(dLambda);
  final phi3 = atan2(sin(phi1) + sin(phi2),
      sqrt((cos(phi1) + bx) * (cos(phi1) + bx) + by * by));
  final lambda3 = lambda1 + atan2(by, cos(phi1) + bx);

  // phi3 and lambda3 are actually in radians and LatLng wants degrees
  return LatLng(
    phi3 * radians2Degrees,
    (lambda3 * radians2Degrees + 540) % 360 - 180,
  );
}