我有一个有效的 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 ?我浏览了一些互联网,但大多数关于它的文献都很混乱。
来自Google Code FAQ - Creating a Store Locator with PHP, MySQL & Google Maps:
下面的 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;