MySQL大圆距离(Haversine公式)我有一个可以工作的PHP脚本,它获取经度和纬度值,然后将它们输入到MySQL查询中。我只想让它成为MySQL。下面是我当前的PHP代码:if ($distance != "Any" && $customer_zip != "") { //get the great circle distance
//get the origin zip code info
$zip_sql = "SELECT * FROM zip_code WHERE zip_code = '$customer_zip'";
$result = mysql_query($zip_sql);
$row = mysql_fetch_array($result);
$origin_lat = $row['lat'];
$origin_lon = $row['lon'];
//get the range
$lat_range = $distance/69.172;
$lon_range = abs($distance/(cos($details[0]) * 69.172));
$min_lat = number_format($origin_lat - $lat_range, "4", ".", "");
$max_lat = number_format($origin_lat + $lat_range, "4", ".", "");
$min_lon = number_format($origin_lon - $lon_range, "4", ".", "");
$max_lon = number_format($origin_lon + $lon_range, "4", ".", "");
$sql .= "lat BETWEEN '$min_lat' AND '$max_lat' AND lon BETWEEN '$min_lon' AND '$max_lon' AND ";
}有谁知道如何把这个变成完整的MySQL吗?我浏览过一些互联网,但它上的大部分文献都令人困惑。
3 回答
慕斯王
TA贡献1864条经验 获得超2个赞
这是SQL语句,它将找到距离37,-122坐标半径25英里内最接近的20个位置。它根据该行的纬度/经度和目标纬度/经度计算距离,然后只请求距离值小于25的行,按距离对整个查询进行排序,并将其限制为20个结果。要搜索公里而不是英里,请将3959替换为6371。
SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin(radians(lat)) ) ) AS distance FROM markers HAVING distance < 25 ORDER BY distance LIMIT 0 , 20;
慕标琳琳
TA贡献1830条经验 获得超9个赞
$greatCircleDistance = acos( cos($latitude0) * cos($latitude1) * cos($longitude0 - $longitude1) + sin($latitude0) * sin($latitude1));
SELECT acos( cos(radians( $latitude0 )) * cos(radians( $latitude1 )) * cos(radians( $longitude0 ) - radians( $longitude1 )) + sin(radians( $latitude0 )) * sin(radians( $latitude1 )) ) AS greatCircleDistance FROM yourTable;
3959
6371
3440
SELECT id FROM spatialEnabledTable WHERE MBRWithin(ogc_point, GeomFromText('Polygon((0 0,0 3,3 3,3 0,0 0))'))
添加回答
举报
0/150
提交
取消