开发者

How can you find the distance between zip codes in C# or SQL Server?

开发者 https://www.devze.com 2023-02-08 18:39 出处:网络
There must be a standard and effi开发者_JAVA百科cient way to do so, since it\'s obviously a very widely used feature on many websites.Unfortunately zip code distance calculator isn’t included into th

There must be a standard and effi开发者_JAVA百科cient way to do so, since it's obviously a very widely used feature on many websites.


Unfortunately zip code distance calculator isn’t included into the .NET framework. Here are some workaround about that

Calculating the distance between two Zip Codes in C#

DISTANCE WIZARD API


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号