不是计算 2 个城市之间的距离,而是计算 100 英里的边界框,然后您有 4 个浮点变量可以插入数据库 - 浮点比较比数据库中的距离计算快得多。缺点是你在角落里有更多的距离。
PHP函数计算边界框
函数 getBoundingBox($lat_degrees,$lon_degrees,$distance_in_miles)
{
$半径=3963.1;// 以英里为单位的地球
// 轴承
$due_north = 0;
$due_south = 180;
$due_east = 90;
$due_west = 270;
// 将纬度和经度转换为弧度
$lat_r = deg2rad($lat_degrees);
$lon_r = deg2rad($lon_degrees);
// 找到最北端、最南端、最东端和最西端的角 $distance_in_miles
//原始公式来自
// http://www.movable-type.co.uk/scripts/latlong.html
$northmost = asin(sin($lat_r) * cos($distance_in_miles/$radius) + cos($lat_r) * sin ($distance_in_miles/$radius) * cos($due_north));
$southmost = asin(sin($lat_r) * cos($distance_in_miles/$radius) + cos($lat_r) * sin ($distance_in_miles/$radius) * cos($due_south));
$eastmost = $lon_r + atan2(sin($due_east)*sin($distance_in_miles/$radius)*cos($lat_r),cos($distance_in_miles/$radius)-sin($lat_r)*sin($lat_r)) ;
$westmost = $lon_r + atan2(sin($due_west)*sin($distance_in_miles/$radius)*cos($lat_r),cos($distance_in_miles/$radius)-sin($lat_r)*sin($lat_r)) ;
$northmost = rad2deg($northmost);
$southmost = rad2deg($southmost);
$eastmost = rad2deg($eastmost);
$westmost = rad2deg($westmost);
//返回2个点NW角和SE角
返回数组($northmost,$westmost,$southmost,$eastmost);
}
那么你的 SQL 是
SELECT * FROM table WHERE latitude <= $northmost AND longitude >= $westmost AND latitude >= $southmost AND longitude <= $eastmost