邮政编码和半径查找建议

发布于 2024-08-29 15:41:23 字数 1539 浏览 2 评论 0原文

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

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

发布评论

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

评论(4

不回头走下去 2024-09-05 15:41:23

更新:我刚刚在 BBC 新闻网站上看到邮政编码数据将从本月起免费。鉴于此,我会使用这些数据。我会寻找该数据库的 API。

上一个答案:英国邮政编码数据由 皇家邮政。其中包含每个邮政编码的 Lng Lat 数据。皇家邮政数据库的专有性质颇具争议。请参阅此网站了解更多信息。
话虽如此,您可以使用 Google Maps Api 来执行此操作。用户可以在他们居住的地图上输入图钉,您就可以捕获该位置的经度和纬度。它不会为您提供上面指定的所有内容。

这是一篇关于 Google 地理编码的好文章API

Update: I just saw on the BBC News site that the PostCode Data will be Free from this month. In light of this, I would use this data. I would have a look for APIs to this database.

Previous answer: UK Post Code data is supplied at SIGNIFICANT cost by the Royal Mail. This comes with Lng Lat data for each postcode. The proprietry nature of the Royal Mail's database is quite controversial. See this site for more info.
Having said that, you could use Google Maps Api to do this. Users could enter place a pin on the map where they live and you could capture the Lng and Lat of that. It won't give you everything you specified above.

Here is a good article on the Google Geocoding API

你的往事 2024-09-05 15:41:23

至少在美国(我完全不知道任何其他国家/地区的邮政编码格式 - 但如果它们映射到长/纬度,这应该可行),您应该能够使用半正矢公式计算出其中的邮政编码给定的地理空间半径。

简单来说,你必须有一个邮政编码->长/纬度表。然后,根据给定的邮政编码(坐标),您可以通过数学方式确定该点给定距离内的所有纬度和经度坐标 - 然后您获取这些近似坐标并转换回邮政编码,然后查询邮政编码内的成员。

之后,只需将它们绘制在地图上即可。下面将提供比我能提供的更多详细信息,因为它解释了数学并实际上将其转换为有效的 MySQL 查询以帮助您入门:

http://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL

At least within the United States (I am completely ignorant of any other country's postal code format -- but if they map to Long/Lat, this ought to work) you should be able to use the Haversine Formula to work out the ZIP codes within a given geospatial radius.

Simply put, you must have a postal code -> long/lat table. Then, from a given postal code (coordinate) you can mathematically determine all latitude and longitudinal coordinates within a given distance of that point -- then you take those approximate coords and convert back to zip, then query for members within zip codes.

After that, it's a matter of plotting them out on a map. The following will give MUCH more detail than I could, as it explains the math and actually translates it into a working MySQL query to get you started:

http://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL

橙味迷妹 2024-09-05 15:41:23

您在某些时候可能会遇到的一件事是必须计算两点(纬度/经度)对之间的距离。最著名的算法之一是Haversine Forumla。我已经根据我找到的一些 C 代码编写了这个实现(不记得原作者是否注明出处)。它是这样的:

public static double DistanceBetween(LatLng pos1, LatLng pos2, DistanceUnit unit)
{
    double R = 6371;

    switch (unit)
    {
        case DistanceUnit.Miles:
            R = 3960;
            break;
        case DistanceUnit.Kilometers:
            R = 6371;
            break;
        case DistanceUnit.Meters:
            R = 6371000;
            break;
    }

    double dLat = GeoMath.DegreesToRadians(pos2.Latitude - pos1.Latitude);
    double dLon = GeoMath.DegreesToRadians(pos2.Longitude - pos1.Longitude);
    double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
        Math.Cos(GeoMath.DegreesToRadians(pos1.Latitude)) *
        Math.Cos(GeoMath.DegreesToRadians(pos2.Latitude)) *
        Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
    double c = 2 * Math.Asin(Math.Min(1, Math.Sqrt(a)));
    double d = R * c;
    return d;
}

其中 DistantUnit 是一个简单的枚举,而 LatLng 本质上只是一个具有两个双属性的结构或类。

One thing you will probably encounter at some point is having to calculate the distance between two points (latitude/longitude) pairs. One of the best known algorithms is the Haversine Forumla. I've written an implementation of this, based on some C code I found (can't remember the original author to credit them). It goes something like this:

public static double DistanceBetween(LatLng pos1, LatLng pos2, DistanceUnit unit)
{
    double R = 6371;

    switch (unit)
    {
        case DistanceUnit.Miles:
            R = 3960;
            break;
        case DistanceUnit.Kilometers:
            R = 6371;
            break;
        case DistanceUnit.Meters:
            R = 6371000;
            break;
    }

    double dLat = GeoMath.DegreesToRadians(pos2.Latitude - pos1.Latitude);
    double dLon = GeoMath.DegreesToRadians(pos2.Longitude - pos1.Longitude);
    double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
        Math.Cos(GeoMath.DegreesToRadians(pos1.Latitude)) *
        Math.Cos(GeoMath.DegreesToRadians(pos2.Latitude)) *
        Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
    double c = 2 * Math.Asin(Math.Min(1, Math.Sqrt(a)));
    double d = R * c;
    return d;
}

Where DistantUnit is a simple enum and LatLng is essentially just a struct or class with two double properties.

皓月长歌 2024-09-05 15:41:23

几个月前我也做过类似的事情。给定一个 IP 地址,我需要知道该服务应在我们自己的服务器上运行的封闭城镇/城市。从一个我不再知道的网站上,我得到了世界各地的纬度/经度/城市位置列表。该表包含大约 8 个 mio 条目。

正如 Dan Diplo 建议的那样,您需要距离来进行此类查询。我的查询是找到给定纬度/经度坐标的最近位置。
使用与 Dan Diplo 类似的函数,我创建了一个索引,用于计算数据库条目 (latlng1) 和零 (latlng2) 的距离。该索引仅用于进行索引扫描以减少数据集 - 它无助于找到最终值。最后,您必须查询您搜索的数据以及到零的距离以减少搜索集。

因此一次查询花费了大约 100 毫秒。但是你必须使用 Diplo 的函数作为 SQL 索引,这对于当前提供数学函数的数据库来说不应该是问题。

I did something similar few months ago. Given an IP address i needed to know the closes town/city where this service should run on our own servers. From a website i dont know any more i got a list of lat/lng/city locations all around the world. The table contained around 8 mio entries.

As Dan Diplo suggest you will need the distance for such queries. My Query was to find the closest location for a given lat/lng coordinate.
Using a similar function as Dan Diplo's i created an index which calculated the distance of the DB entry (latlng1) and zero (latlng2). This index is only used to get an index scan to reduce the data set - it doestn help finding the final value. Finally you have to query for the data you search AND the distance to zero to reduce the search set.

Thus a query took around 100ms. But you have to use Diplo's function as a SQL index which shouldnt be a problem on current databases providing mathematical functions.

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