PHP&MySQL 商店距离计算器 - 邮政编码

发布于 2024-11-03 13:07:46 字数 298 浏览 1 评论 0原文

是的,我一直在尝试找出如何将给定的邮政编码与商店地址的数据库进行比较,并根据最接近给定的邮政编码(或我猜的邮政编码)的方式对它们进行排序。

这主要是出于兴趣,而不是我向您寻求建议然后将其出售给客户:-O

首先,经过研究,我发现您必须与纬度/经度进行距离计算,所以我找到了一个可以转换邮政编码/的API邮政编码转换为经纬度,现在我的数据库具有诸如id,store_name,lat,long,postcode之类的结构,我可以将给定的邮政编码转换为经纬度。

但是,如何在 SQL 中查询最接近给定经纬度的值呢?

Right I have been trying to work out how to compare a given postcode to a database of say store addresses and have them ordered in terms of which one is closest to the given postcode (or ZIP code I guess).

This is mainly out of interest, rather than me asking you for advice and then selling it to a client :-O

First of all after research I discovered that you have to do distance with Lat/Long so I found an API that converts postcodes/zip codes to lat long and now my DB has a structure such as id, store_name, lat, long, postcode and I can convert a given postcode to a lat long.

But how in SQL do I make a query for the ones closest to a given lat long?

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

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

发布评论

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

评论(3

栖迟 2024-11-10 13:07:46

尝试这样的事情:

// get all the zipcodes within the specified radius - default 20
    function zipcodeRadius($lat, $lon, $radius)
    {
        $radius = $radius ? $radius : 20;
        $sql = 'SELECT distinct(ZipCode) FROM zipcode  WHERE (3958*3.1415926*sqrt((Latitude-'.$lat.')*(Latitude-'.$lat.') + cos(Latitude/57.29578)*cos('.$lat.'/57.29578)*(Longitude-'.$lon.')*(Longitude-'.$lon.'))/180) <= '.$radius.';';
        $result = $this->db->query($sql);
        // get each result
        $zipcodeList = array();
        while($row = $this->db->fetch_array($result))
        {
            array_push($zipcodeList, $row['ZipCode']);
        }
        return $zipcodeList;
    }

更新:
有一些关于效率的讨论。这是针对此查询的一个小基准。我有一个包含美国每个邮政编码的数据库。由于邮政编码的工作方式,其中一些是重复的(超出了本主题的范围)。所以我有不到 80k 条记录。我在 90210 上跑了 20 英里半径的距离:

SELECT distinct(ZipCode) FROM zipcodes  WHERE (3958*3.1415926*sqrt((Latitude-34.09663010)*(Latitude-34.09663010) + cos(Latitude/57.29578)*cos(34.09663010/57.29578)*(Longitude- -118.41242981)*(Longitude- -118.41242981))/180) <= 20

我得到了总共 366 条记录,并且查询花费了0.1770 秒。您需要提高多少效率?

Try something like this:

// get all the zipcodes within the specified radius - default 20
    function zipcodeRadius($lat, $lon, $radius)
    {
        $radius = $radius ? $radius : 20;
        $sql = 'SELECT distinct(ZipCode) FROM zipcode  WHERE (3958*3.1415926*sqrt((Latitude-'.$lat.')*(Latitude-'.$lat.') + cos(Latitude/57.29578)*cos('.$lat.'/57.29578)*(Longitude-'.$lon.')*(Longitude-'.$lon.'))/180) <= '.$radius.';';
        $result = $this->db->query($sql);
        // get each result
        $zipcodeList = array();
        while($row = $this->db->fetch_array($result))
        {
            array_push($zipcodeList, $row['ZipCode']);
        }
        return $zipcodeList;
    }

UPDATE:
There is some discussion about efficiency. Here is a little benchmark for you with this query. I have a database that contains EVERY zipcode in the US. Some of them are duplicate because of the way zipcodes work (outside the scope of this topic). So I have just under 80k records. I ran a 20 mile radius distance on 90210:

SELECT distinct(ZipCode) FROM zipcodes  WHERE (3958*3.1415926*sqrt((Latitude-34.09663010)*(Latitude-34.09663010) + cos(Latitude/57.29578)*cos(34.09663010/57.29578)*(Longitude- -118.41242981)*(Longitude- -118.41242981))/180) <= 20

I got back 366 total records and Query took 0.1770 sec. How much more efficient do you need?

心的憧憬 2024-11-10 13:07:46

查看这个伟大的开源项目

免责声明:不是我的项目,我也不是贡献者。纯粹是推荐。

check out this great open source project

Disclaimer: Not my project, and nor am I contributor. Purely a recommendation.

惜醉颜 2024-11-10 13:07:46

有关示例,请参阅对上一个问题的回答在查询 MySQL 之前计算边界框。这允许 MySQL 查询中的复杂公式针对数据库条目的子集运行,而不是针对表中的每个条目。

See this answer to a previous question for an example of calculating a bounding box before querying MySQL. This allows the complex formula in the MySQL query to run against a subset of the database entries, rather than against every entry in the table.

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