php - MySQL 大圆距离(半正矢公式)

标签 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 大圆距离(半正矢公式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54585776/

相关文章:

javascript - 在 laravel 中通过 javascript 获取信息

mysql - 将行转换为带有ID的mySQL列(不旋转)

javascript - 奇怪的大圆距离计算

php - 实现大圆算法

line - 点到线的距离大圆函数无法正常工作。

具有多个查询的 php 函数 - 内部连接复制结果?

php - 使用 REGEX/PHP 删除标签和中间内容

php - PHP PDO SQlite单个查询仅查找一次结果

php - MySQL UPDATE 查询(PDO)莫名其妙地不起作用

mysql - 通过 LAN 连接到 mySQL