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 = southWest.latitudeInRad;
  final lambda1 = southWest.longitudeInRad;
  final phi2 = northEast.latitudeInRad;

  final dLambda = degToRadian(northEast.longitude -
      southWest.longitude); // delta lambda = lambda2-lambda1

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

  // phi3 and lambda3 are actually in radians and LatLng wants degrees
  return LatLng(radianToDeg(phi3), radianToDeg(lambda3));
}