且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

按距离对邮政编码邻近度搜索进行排序(php/mysql)

更新时间:2021-08-01 17:18:01

您可以使用类似

$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