按距离对邮政编码邻近度搜索进行排序(php/mysql) [英] Sorting zip code proximity search by distance (php/mysql)
问题描述
我有一个表(user_zip_codes),其中包含用户的邮政编码,纬度和经度.我在stackoverflow上找到了一个函数,该函数可查找特定半径内的邮政编码:
I have a table (user_zip_codes) with the users' zip_code, latitude and longitude. I have found a function here on stackoverflow that finds zip codes within a specific radius:
function zipcodeRadius($lat, $lon, $radius) {
global $mysqli;
$zipcodeList = array();
$radius = ($radius ? $radius : 20);
$sql = "SELECT userID,city,zip_code,country FROM user_zip_codes WHERE (3958*3.1415926*sqrt((lat-$lat)*(lat-$lat) + cos(lat/57.29578)*cos($lat/57.29578)*(lon-$lon)*(lon-$lon))/180) <= $radius GROUP BY zip_code";
if($stmt = $mysqli->prepare($sql)) {
$stmt->execute();
$stmt->bind_result($userID,$city,$zip_code,$country);
while($stmt->fetch()) {
$zipcodeList[] = array('userID'=>$userID,'city'=>$city,'zip_code'=>$zip_code,'country'=>$country);
}
}
return $zipcodeList;
}
它完美地工作.但是,我希望该函数按距离对数组进行排序(通过ASC或DESC).我应该如何调整查询以使其发生?
It works perfectly. However, I would like the function to sort the array by distance (either by ASC og DESC). How should I adjust my query in order for this to happen?
谢谢.
更新: 距离"一词可能会出现歧义(这要归功于豪尔赫).我只是希望按距离(即两点之间的距离)对邮政编码进行排序.
UPDATE: The word 'distance' might appear ambiguous (thanks to Jorge). I simply wish to sort the zip_codes by distance meant as the distance between two points.
推荐答案
您可以使用类似
$iDistance = 20;
$iRadius = 6371; // earth radius in km
$iRadius = 3958; // earth radius in miles
$fLat = x.y; // Your position latitude
$fLon = x.y; // Your position longitude
$strQuery = "
SELECT
*,
$iRadius * 2 * ASIN(SQRT(POWER(SIN(( $fLat - abs(pos.lat)) * pi() / 180 / 2),2) +
COS( $fLat * pi()/180) * COS(abs(pos.lat) * pi() / 180) * POWER(SIN(( $fLon - pos.lon) *
pi() / 180 / 2), 2) )) AS distance
FROM user_zip_codes pos
HAVING distance < $iDistance
ORDER BY distance";
在使用SQL之前必须获取纬度/经度值.这对我有用
where you have to fetch your lat/lon value before using the SQL. This works for me
这篇关于按距离对邮政编码邻近度搜索进行排序(php/mysql)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!