Php: determining latitude and longitude boundaries based on central lat / lng and distance

I worked with a very similar question and after implementation I found that it creates a tall rectangle with the returned coordinates instead of a square (please see Matthias answer to another question).

I only need an array that needs to be returned, as this should work with WordPress, which has its own preferred request method.

Here is my implementation:

function bar_get_nearby( $lat, $lng, $limit = 50, $distance = 50, $unit = 'mi' ) { // radius of earth; @note: the earth is not perfectly spherical, but this is considered the 'mean radius' if( $unit == 'km' ) { $radius = 6371.009; } elseif ( $unit == 'mi' ) { $radius = 3958.761; } // latitude boundaries $maxLat = ( float ) $lat + rad2deg( $distance / $radius ); $minLat = ( float ) $lat - rad2deg( $distance / $radius ); // longitude boundaries (longitude gets smaller when latitude increases) $maxLng = ( float ) $lng + rad2deg( $distance / $radius / cos( deg2rad( ( float ) $lat ) ) ); $minLng = ( float ) $lng - rad2deg( $distance / $radius / cos( deg2rad( ( float ) $lat ) ) ); $max_min_values = array( 'max_latitude' => $maxLat, 'min_latitude' => $minLat, 'max_longitude' => $maxLng, 'min_longitude' => $minLng ); return $max_min_values; } 

If I give the geocode (via the Google Maps API) my desired G2 1QX zip code and a distance of 5 miles, I get lat / lng from -4.2556347 / 55.8620472 with a function returning this array:

 Array ( [max_latitude] => -4.18326890233 [min_latitude] => -4.32800049767 [max_longitude] => 55.9346130696 [min_longitude] => 55.7894813304 ) 

Any ideas? Thank you very much in advance.

Cheers, Robert

+7
source share
2 answers

Here is your function with my modifications in it, which will give you square coordinates instead of a high rectangle:

 function bar_get_nearby( $lat, $lng, $limit = 50, $distance = 50, $unit = 'mi' ) { // radius of earth; @note: the earth is not perfectly spherical, but this is considered the 'mean radius' if( $unit == 'km' ) { $radius = 6371.009; } elseif ( $unit == 'mi' ) { $radius = 3958.761; } // latitude boundaries $maxLat = ( float ) $lat + rad2deg( $distance / $radius ); $minLat = ( float ) $lat - rad2deg( $distance / $radius ); // longitude boundaries (longitude gets smaller when latitude increases) $maxLng = ( float ) $lng + rad2deg( $distance / $radius) / cos( deg2rad( ( float ) $lat ) ); $minLng = ( float ) $lng - rad2deg( $distance / $radius) / cos( deg2rad( ( float ) $lat ) ); $max_min_values = array( 'max_latitude' => $maxLat, 'min_latitude' => $minLat, 'max_longitude' => $maxLng, 'min_longitude' => $minLng ); return $max_min_values; } 

Greetings

Rupesh Kamble

+4
source

I once wrote this function to calculate the distance between two points in kilometers (km) instead of miles. I wrote a brief modification to answer a mile.

 /** * The Haversine function can be used to calculate the distance between 2 points on a map * * @param float $lat1 The longtitude value of the first point * @param float $lon1 The lattitude of the frist point * @param float $lat2 The longtitude value of the second point * @param float $lon2 The lattitude of the second point * @return float The distance between the points in mile * * @access public **/ public function harversineDistance($lat1, $lon1, $lat2, $lon2) { $latd = deg2rad($lat2 - $lat1); $lond = deg2rad($lon2 - $lon1); $a = sin($latd / 2) * sin($latd / 2) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * sin($lond / 2) * sin($lond / 2); $c = 2 * atan2(sqrt($a), sqrt(1 - $a)); // Original return for the km answer //return 6371.0 * $c; // Return for the mile answer on 2 digits percision return round(((6371.0 * $c) * 0.621371192), 2); } 
0
source

All Articles