使用 MySQL SELECT AS 获取当前纬度/经度
我正在尝试检索靠近用户当前纬度/经度位置的列表。
已尝试 MySQL 的地理空间 POINT 无济于事,因此返回对每条记录使用 DECIMAL 纬度/经度值。
以下函数旨在获取 10 公里内的列表(基于 这个公式)我意识到 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"];
}
}
I'm trying to retrieve listings that are near a user's current lat/long position.
Have tried MySQL's geospatial POINT to no avail, so returning to using DECIMAL lat/lon values for each record.
The below function is meant to get the listings within 10km (based on this formula) I realise the SELECT isn't quite right, but I'm not sure how to define the ((ACOS ... 1.1515) as distance and then also select the other columns.
// 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"];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试查看 MySQL 大圆距离。您还可以查看@ Google 地图 SQL api 。它展示了如何查找另一个位置附近的位置。
现在专门查看您的代码,您发现了一些错误。
语句后没有分号:
$current_lon = 175.505
$result = mysql_query("
没有字符串连接(您在字符串中使用双引号,但也在字符串内部使用双引号)。
}
。以下是代码的快速清理:
You can try looking into MySQL great circle distance. And you can also take a look @ Google maps SQL api. It shows how to find a location near another location.
Now looking specifically at your code you have a couple of errors.
No semicolon after statements:
$current_lon = 175.505
$result = mysql_query("
No concatenation of strings (you use double quotes for your strings but also inside them).
No
}
after your function ends.Here's a quick cleanup of your code: