使用 MySQL 空间数据获取 Google 地图上最近的地点

发布于 2024-10-11 12:35:13 字数 116 浏览 6 评论 0原文

我有一个数据库,其中包含商店列表以及每个商店的纬度和经度。因此,根据我输入的当前(纬度、经度)位置,我想获取 1 公里、5 公里等半径范围内的项目列表?

算法应该是什么?我需要算法本身的 PHP 代码。

I have a database with a list of stores with latitudes and longitudes of each. So based on the current (lat, lng) location that I input, I would like to get a list of items from those within some radius like 1 km, 5km etc?

What should be the algorithm? I need the PHP code for algorithm itself.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

眼泪淡了忧伤 2024-10-18 12:35:13

您只需要使用以下查询。

例如,您输入纬度和经度 37 和 -122(以度为单位)。您想要搜索距离当前给定纬度和经度 25 英里以内的用户。

SELECT item1, item2, 
    ( 3959 * acos( cos( radians(37) ) 
                   * cos( radians( lat ) ) 
                   * cos( radians( lng ) 
                       - radians(-122) ) 
                   + sin( radians(37) ) 
                   * sin( radians( lat ) ) 
                 )
   ) AS distance 
FROM geocodeTable 
HAVING distance < 25 
ORDER BY distance LIMIT 0 , 20;

如果您想要以公里为单位的搜索距离,请在上面的查询中将 3959 替换为 6371。

您还可以执行以下操作:

  1. 选择所有纬度和经度

  2. 然后计算每个记录的距离。

  3. 上述过程可以通过多重重定向来完成。

为了优化查询,您可以使用存储过程。

这个也可以帮助您。

You just need use following query.

For example, you have input latitude and longitude 37 and -122 in degrees. And you want to search for users within 25 miles from current given latitude and longitude.

SELECT item1, item2, 
    ( 3959 * acos( cos( radians(37) ) 
                   * cos( radians( lat ) ) 
                   * cos( radians( lng ) 
                       - radians(-122) ) 
                   + sin( radians(37) ) 
                   * sin( radians( lat ) ) 
                 )
   ) AS distance 
FROM geocodeTable 
HAVING distance < 25 
ORDER BY distance LIMIT 0 , 20;

If you want search distance in kms, then replace 3959 with 6371 in above query.

You can also do this like:

  1. Select all Latitude and longitude

  2. Then calculate the distance for each record.

  3. The above process can be done with multiple redirection.

For optimizing query you can use Stored Procedure.

And this can also help you.

空名 2024-10-18 12:35:13

您应该选择一个支持空间的数据库,例如 mysqlpostgresql 然后您可以使用它们提供的一些现成函数。
否则,如果您想手动执行此操作,请检查 这个 供大家注意。

You should choose a database that is spatially enabled like mysql or postgresql and then you can use some of the ready functions they providing.
Else if you want to do it manually check this for heads up.

妄断弥空 2024-10-18 12:35:13

如果您正在寻找用于计算两组坐标之间的距离的 PHP 代码,这里有一个我改编的类,它将计算以公里为单位的距离。但是,如果您使用数据库,我建议您检查一下您的数据库是否能够进行空间计算(我知道 SQL Server 和 MySQL 是这样的,这是我无法想象的)。

这里有一个有趣的 SQL 解决方案链接,您可能想查看一下。 在 PHP 中优化半正矢公式 SQL 调用

class Distance
{
    /**
     * Mean raidus of the earth in kilometers.
     * @var double
     */
    const RADIUS    = 6372.797;

    /**
     * Pi divided by 180 degrees. Calculated with PHP Pi constant.
     * @var double
     */
    const PI180         = 0.017453293;

    /**
     * Constant for converting kilometers into miles.
     * @var double
     */
    const MILES     = 0.621371192;

    /**
     * Calculate distance between two points of latitude and longitude.
     * @param double $lat1 The first point of latitude.
     * @param double $long1 The first point of longitude.
     * @param double $lat2 The second point of latitude.
     * @param double $long2 The second point of longitude.
     * @param bool $kilometers Set to false to return in miles.
     * @return double The distance in kilometers or miles, whichever selected.
     */
    public static function getDistance($lat1, $long1, $lat2, $long2, $kilometers = true)
    {
        $lat1   *= self::PI180;
        $long1  *= self::PI180;
        $lat2   *= self::PI180;
        $long2  *= self::PI180;

        $dlat = $lat2 - $lat1;
        $dlong = $long2 - $long1;

        $a = sin($dlat / 2) * sin($dlat / 2) + cos($lat1) * cos($lat2) * sin($dlong / 2) * sin($dlong / 2);
        $c = 2 * atan2(sqrt($a), sqrt(1 - $a));

        $km = self::RADIUS * $c;

        if($kilometers)
        {
            return $km;
        }
        else
        {
            return $km * self::MILES;
        }
    }
}

//example
echo Distance::getDistance(40.686748, -89.555054, 40.453078, -88.939819);

If you're looking for PHP code for calculating distance between two sets of coordinates here's a class that I adapted that will calculate the distance in kilometers. However, if you're using a database I would suggest that you look into whether or not your database is capable of spacial computations (I know that SQL Server and MySQL are, off the top of my head).

Here's an interesting link for a SQL solution that you might want to check out. Optimising a haversine formula SQL call in PHP

class Distance
{
    /**
     * Mean raidus of the earth in kilometers.
     * @var double
     */
    const RADIUS    = 6372.797;

    /**
     * Pi divided by 180 degrees. Calculated with PHP Pi constant.
     * @var double
     */
    const PI180         = 0.017453293;

    /**
     * Constant for converting kilometers into miles.
     * @var double
     */
    const MILES     = 0.621371192;

    /**
     * Calculate distance between two points of latitude and longitude.
     * @param double $lat1 The first point of latitude.
     * @param double $long1 The first point of longitude.
     * @param double $lat2 The second point of latitude.
     * @param double $long2 The second point of longitude.
     * @param bool $kilometers Set to false to return in miles.
     * @return double The distance in kilometers or miles, whichever selected.
     */
    public static function getDistance($lat1, $long1, $lat2, $long2, $kilometers = true)
    {
        $lat1   *= self::PI180;
        $long1  *= self::PI180;
        $lat2   *= self::PI180;
        $long2  *= self::PI180;

        $dlat = $lat2 - $lat1;
        $dlong = $long2 - $long1;

        $a = sin($dlat / 2) * sin($dlat / 2) + cos($lat1) * cos($lat2) * sin($dlong / 2) * sin($dlong / 2);
        $c = 2 * atan2(sqrt($a), sqrt(1 - $a));

        $km = self::RADIUS * $c;

        if($kilometers)
        {
            return $km;
        }
        else
        {
            return $km * self::MILES;
        }
    }
}

//example
echo Distance::getDistance(40.686748, -89.555054, 40.453078, -88.939819);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文