使用 MySQL SELECT AS 获取当前纬度/经度

发布于 2024-10-31 16:05:29 字数 1269 浏览 1 评论 0原文

我正在尝试检索靠近用户当前纬度/经度位置的列表。

已尝试 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逐鹿 2024-11-07 16:05:29

您可以尝试查看 MySQL 大圆距离。您还可以查看@ Google 地图 SQL api 。它展示了如何查找另一个位置附近的位置。

现在专门查看您的代码,您发现了一些错误。

  1. 语句后没有分号:

    $current_lon = 175.505
    $result = mysql_query("

  2. 没有字符串连接(您在字符串中使用双引号,但也在字符串内部使用双引号)。

  3. < p>函数结束后没有 }

以下是代码的快速清理:

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;
}

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.

  1. No semicolon after statements:

    $current_lon = 175.505
    $result = mysql_query("

  2. No concatenation of strings (you use double quotes for your strings but also inside them).

  3. No } after your function ends.

Here's a quick cleanup of your code:

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文