【发布时间】:2023-03-05 08:28:01
【问题描述】:
我正在尝试检索靠近用户当前纬度/经度位置的列表。
已尝试 MySQL 的地理空间 POINT 无济于事,因此返回为每条记录使用 DECIMAL lat/lon 值。
以下函数旨在获取 10 公里内的列表(基于 this formula)我意识到 SELECT 不太正确,但我不确定如何将 ((ACOS ... 1.1515) 定义为距离,然后还选择其他列。
// functions.php
function getDeals($type_of_deal) {
$current_lat = -37.905;
$current_lon = 175.505
$result = mysql_query("
SELECT i, company,
TIME_FORMAT(valid_time_start, '%l:%i %p'),
TIME_FORMAT(valid_time_end, '%l:%i %p'),
DATE_FORMAT(valid_expiry, '%D %b %y'),
((ACOS(SIN("$current_lat" * PI() / 180) * SIN(`company_lat` * PI() / 180) +
COS("$current_lat" * PI() / 180) * COS(`company_lat` * PI() / 180) *
COS(("$current_lon" – `company_lon`) * PI() / 180)) * 180 / PI()) * 60 * 1.1515)
AS distance
FROM listings
WHERE deal_type = '" .$type_of_deal. "'
HAVING distance<=’10′
ORDER BY `distance` ASC
");
return $result;
// location.php
$type_of_deal = food;
$result = getDeals($type_of_deal);
if (mysql_num_rows($result)) {
while($row = mysql_fetch_array($result)) {
echo $row["deal_title"];
}
}
【问题讨论】:
标签: php mysql geolocation gps