calculateBoundingBox static method
Calculates the bounding box (rectangle) around a center point with a specified distance in kilometers.
centerPoint
- The LatLng coordinates of the center point.
distanceInKm
- The distance in kilometers from the center point to each side
of the bounding box.
Returns a list of two LatLng points representing the top-left and bottom-right corners of the bounding box.
Implementation
static List<LatLng> calculateBoundingBox(
LatLng centerPoint, num distanceInKm) {
// Earth's radius in kilometers
final num radiusOfEarth = 6371e3 / 1000;
// Convert latitude to radians
final num latInRadians = centerPoint.latitude * (pi / 180.0);
final num degreeLatDistance = (distanceInKm / radiusOfEarth) * (180.0 / pi);
final num degreeLngDistance = degreeLatDistance / cos(latInRadians);
final num topLat = centerPoint.latitude + degreeLatDistance;
final num leftLng = centerPoint.longitude - degreeLngDistance;
final num bottomLat = centerPoint.latitude - degreeLatDistance;
final num rightLng = centerPoint.longitude + degreeLngDistance;
final LatLng topLeft = LatLng(topLat.toDouble(), leftLng.toDouble());
final LatLng bottomRight =
LatLng(bottomLat.toDouble(), rightLng.toDouble());
return [topLeft, bottomRight];
}