PHP library. Compute the bounding rectangle for the given lat / lng location.
The function is wrong after the // bearing bit, it should be like this:
function getBoundingBox($lat_degrees,$lon_degrees,$distance_in_miles) {
$radius = 3963.1; // of earth in miles
// bearings - FIX
$due_north = deg2rad(0);
$due_south = deg2rad(180);
$due_east = deg2rad(90);
$due_west = deg2rad(270);
// convert latitude and longitude into radians
$lat_r = deg2rad($lat_degrees);
$lon_r = deg2rad($lon_degrees);
// find the northmost, southmost, eastmost and westmost corners $distance_in_miles away
// original formula from
// http://www.movable-type.co.uk/scripts/latlong.html
$northmost = asin(sin($lat_r) * cos($distance_in_miles/$radius) + cos($lat_r) * sin ($distance_in_miles/$radius) * cos($due_north));
$southmost = asin(sin($lat_r) * cos($distance_in_miles/$radius) + cos($lat_r) * sin ($distance_in_miles/$radius) * cos($due_south));
$eastmost = $lon_r + atan2(sin($due_east)*sin($distance_in_miles/$radius)*cos($lat_r),cos($distance_in_miles/$radius)-sin($lat_r)*sin($lat_r));
$westmost = $lon_r + atan2(sin($due_west)*sin($distance_in_miles/$radius)*cos($lat_r),cos($distance_in_miles/$radius)-sin($lat_r)*sin($lat_r));
$northmost = rad2deg($northmost);
$southmost = rad2deg($southmost);
$eastmost = rad2deg($eastmost);
$westmost = rad2deg($westmost);
// sort the lat and long so that we can use them for a between query
if ($northmost > $southmost) {
$lat1 = $southmost;
$lat2 = $northmost;
} else {
$lat1 = $northmost;
$lat2 = $southmost;
}
if ($eastmost > $westmost) {
$lon1 = $westmost;
$lon2 = $eastmost;
} else {
$lon1 = $eastmost;
$lon2 = $westmost;
}
return array($lat1,$lat2,$lon1,$lon2);
}
I got this feature thanks to: http://xoxco.com/clickable/php-getboundingbox
a source to share
This is a relatively simple problem to solve if you assume the bounding box runs east-west in one direction and north-south in the other. You can do latitude and longitude independently.
For latitude, sort the points from west to east. At this point, you should treat the list as a circular buffer. You need to check each point and find the one that is at the farthest next point. So, suppose ten points a 0 for nineif a 4 and five most distant, limiting the latitude of the field from five round up 4... Name them w and e
For longitude, you just need to find the northernmost and southernmost points, name them n and s...
Longitude w and e and latitude n and s define the bounding box.
a source to share