Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

sql

zip

wpf

There must be a standard and efficient way to do so, since it's obviously a very widely used feature on many websites.

like image 690
Scifiballer24 Avatar asked Dec 22 '22 18:12

Scifiballer24


2 Answers

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

like image 167
Arsen Mkrtchyan Avatar answered Jan 28 '23 14:01

Arsen Mkrtchyan


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.

like image 43
Merlyn Morgan-Graham Avatar answered Jan 28 '23 13:01

Merlyn Morgan-Graham