php: finding latitude and longitude boundaries based on a central lat/lng and distance php: finding latitude and longitude boundaries based on a central lat/lng and distance php php

php: finding latitude and longitude boundaries based on a central lat/lng and distance


Here is your function with my Modifications in it that will give you a square coordinates instead of tall 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;}

Cheers,

Rupesh Kamble


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

/** * 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);}


i need this same function sql so i convert it for sql statement and this is how we can call using sql query

select bar_get_nearby(4.860416,-58.93018, 100 ,@"mi")

special thanks to Rupesh K

DELIMITER $$CREATE FUNCTION bar_get_nearby(lat FLOAT,lng FLOAT,distance FLOAT,unit VARCHAR(3)) RETURNS TEXTBEGIN  DECLARE radius FLOAT;  DECLARE maxLat FLOAT;  DECLARE minLat FLOAT;  DECLARE maxLng FLOAT;  DECLARE minLng FLOAT;  DECLARE max_min_values TEXT;  CASE unit    WHEN  'km' THEN       SET radius = 6371.009;    WHEN 'mi' THEN       SET radius = 3958.761;    ELSE       SET radius = 3958.761;  END CASE;   SET maxLat = lat + degrees( distance / radius );   SET minLat = lat - degrees( distance / radius );   SET maxLng = lng + degrees( distance / radius) / COS( radians( lat ) );   SET minLng = lng - degrees( distance / radius) / COS( radians( lat ) );    SET max_min_values = concat('north=' , maxLat, '&south=' , minLat,'&east=' , maxLng, '&west=' , minLng);  RETURN max_min_values;END$$DELIMITER ;