php - MySQL 大圆距离(Haversine 公式)

标签 php mysql great-circle

我有一个有效的 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 :

Here's the SQL statement that will find the closest 20 locations that are within a radius of 25 miles to the 37, -122 coordinate. It calculates the distance based on the latitude/longitude of that row and the target latitude/longitude, and then asks for only rows where the distance value is less than 25, orders the whole query by distance, and limits it to 20 results. To search by kilometers instead of miles, replace 3959 with 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;

关于php - MySQL 大圆距离(Haversine 公式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/574691/

相关文章:

php - MySQL 显示奇怪的结果

php - Laravel 等效查询字段 +1 插入数据

algorithm - 点与多边形之间的距离

java - sql - 创建单个查询以获取给定条件的值

c# - 如何计算最短路线?

php - 大圆距离问题

php - 我有一个 require ("config.php") 和数组,但仍然出现 undefined variable 错误

PHP 和 Paypal 。单击提交时,将数据保存到数据库并重定向到 paypal

php - 多张图片上传在 iphone 和 ipad 中不起作用

mysql - 检查mysql表中的年龄