如何在 C# 或 SQL Server 中找到邮政编码之间的距离?
必须有一个标准且有效的方法来做到这一点,因为它显然是许多网站上使用非常广泛的功能。
There must be a standard and efficient way to do so, since it's obviously a very widely used feature on many websites.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道有一个“标准”的方式。
可能有现有的服务可以做到这一点,并且可能有您可以购买或免费获得的包含可用数据的数据库,但根据您的定义,这可能不容易计算。
想想看:您的问题可以解释为两点之间的最短距离(通过使用位于该邮政编码的城市中的点进行作弊),或者可以解释为两点之间的驾驶距离两个邮政编码边界的边缘。第一个计算相当简单,但并不是 100% 准确。第二个问题并不是一个简单的问题。找到最短驾驶距离是一个子问题,找到不规则边界边缘的最近点是另一个子问题(或找到调用该特征的人的最近城市/已知坐标)。
因此,在没有任何有关您要解决的实际问题的信息的情况下,我只能假设您不关心数据的准确性。在这种情况下,我建议获取城市坐标数据库(它们所在的邮政编码),并使用标准减法/平方根来确定距离(毕达哥拉斯定理)。
如果您确实关心数据的准确性,我建议您研究一下,找到一个现有的数据库或服务,其中包含为您预先计算或为您提供的更准确的信息。
I don't know that there is a "standard" way.
There may be existing services that do it, and there may be databases you can purchase or get for free that have usable data, but depending on your definition, this may not be easy to calculate.
Think about this: Your question could be interpreted as the shortest as-the-crow-flies distance between two points (cheating by using a point in a city that lies in that zip code), or it could be interpreted as the driving distance between the edge of two zip code boundaries. The first calculation is fairly simple, but isn't 100% accurate. The second is not a trivially easy problem. Finding the shortest driving distance is one sub-problem, and finding the closest points at the edge of irregular boundaries is another sub-problem (or finding the nearest city/known coordinates of the person invoking the feature).
So, without any information as to the actual problem you are trying to solve, I can only assume you don't care how accurate your data is. In that case, I suggest acquiring a database of coordinates of cities, which zip codes they lie in, and use the standard subtractions/square-root to determine the distance (pythagorean theorem).
If you do care how accurate your data is, I suggest you research to find an existing database or service that has more accurate information pre-calculated for you, or farmed to you.
我以前写过一个 PHP 脚本来计算邮政编码之间的距离。此后,通过大量用户反馈,对其进行了修改,距离计算实际上是通过 SQL 查询完成的,因为这是最有效的方法。
您需要一个包含邮政编码及其纬度/经度值的表。网络上有免费和商业数据库提供此信息。
查看 此 PHP 源代码 并查看
calcDistanceSql()
方法。它应该足够简单,可以遵循在 SQL Server 中构建并实现它的查询。I wrote a PHP script way back to calculate the distance between zip codes. Since then, through a lot of feedback from users, it has been modified so that the distance calculation is actually done with SQL queries as that was the most efficient way to do it.
You need a table that has zip codes and their lat/long values. There are free and commercial databases with this information available all over the web.
Take a look at this PHP source code and look at the
calcDistanceSql()
method. It should be simple enough to follow the query that is built and implement it in SQL Server.