center property

LatLng get 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

  var phi1 = southWest!.latitudeInRad;
  var lambda1 = southWest!.longitudeInRad;
  var phi2 = northEast!.latitudeInRad;

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

  var bx = math.cos(phi2) * math.cos(dLambda);
  var by = math.cos(phi2) * math.sin(dLambda);
  var phi3 = math.atan2(math.sin(phi1) + math.sin(phi2),
      math.sqrt((math.cos(phi1) + bx) * (math.cos(phi1) + bx) + by * by));
  var 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));
}