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

标签 php mysql geolocation gps

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

已尝试 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 .它展示了如何找到另一个位置附近的位置。

现在具体查看您的代码,您会发现一些错误。

  1. 声明后没有分号:

    $current_lon = 175.505 $result = mysql_query("

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

  3. 函数结束后没有}

这是对您的代码的快速清理:

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/

相关文章:

mysql - 如何查询零件列表,以便可以轻松比较来自许多不同组的零件? (数据透视表?)

javascript - Google Maps API - 使用 "search nearby"功能?

php - 使用 Laravel 从 Amazon S3 下载文件

php - 转换为 raw 的音频文件是否保留以前的采样率?

php - MySQL table1 和 table2 如果表 1 中不存在则显示

php - 如何在laravel中填充数据透视表?

php - 使用php从mysql导出电子邮件,但导出我所有的php页面

mysql - 组合两个 SQL 语句,其中在多对多关系中两者都必须为 true

javascript - 地理定位 API 错误处理

geolocation - 有没有一种方法可以获得不包含国家/地区的世界形状文件?