我正在尝试检索靠近用户当前纬度/经度位置的列表。
已尝试 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"];
}
}
最佳答案
您可以尝试查看 MySQL great circle distance .还有你也可以看看@Google maps SQL api .它展示了如何找到另一个位置附近的位置。
现在具体查看您的代码,您会发现一些错误。
声明后没有分号:
$current_lon = 175.505 $result = mysql_query("
没有字符串连接(您对字符串使用双引号,但也在它们内部)。
函数结束后没有
}
。
这是对您的代码的快速清理:
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;
}
关于php - 使用 MySQL SELECT AS 获取当前纬度/经度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5609926/