[英]php: finding latitude and longitude boundaries based on a central lat/lng and distance
I was working with the solution to a very similar question and upon implementation I discovered that it was producing a tall rectangle with the returned coordinates instead of a square (please see Matthias' answer to the other question).我正在研究一个非常相似的问题的解决方案,在实施时我发现它生成了一个带有返回坐标的高矩形而不是正方形(请参阅 Matthias 对另一个问题的回答)。
I only needed an array to be returned as this is to work with WordPress which has it's own preferred query method.我只需要返回一个数组,因为这是与 WordPress 一起使用的,它有自己的首选查询方法。
Here's 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 I geocode (via Google Maps API) my desired postcode of G2 1QX and a distance of 5 miles I get a lat/lng of -4.2556347/55.8620472 with the function returning this array:如果我给我地理编码(通过谷歌地图 API)我想要的 G2 1QX 邮政编码和 5 英里的距离,我得到 -4.2556347/55.8620472 的纬度/经度,函数返回这个数组:
Array
(
[max_latitude] => -4.18326890233
[min_latitude] => -4.32800049767
[max_longitude] => 55.9346130696
[min_longitude] => 55.7894813304
)
Any ideas?有任何想法吗? Many thanks in advance.
提前谢谢了。
Cheers, RS干杯,RS
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.我曾经写过这个函数来计算两点之间的距离,单位为公里(km)而不是英里。 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我需要同样的函数 sql 所以我将它转换为 sql 语句,这就是我们如何使用 sql 查询调用
select bar_get_nearby(4.860416,-58.93018, 100 ,@"mi")
special thanks to Rupesh K特别感谢Rupesh K
DELIMITER $$
CREATE FUNCTION bar_get_nearby(lat FLOAT,lng FLOAT,distance FLOAT,unit VARCHAR(3)) RETURNS TEXT
BEGIN
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 ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.