gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 20:17:55 26 4
gpt4 key购买 nike

我有一个可用的 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/39642949/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com