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

There must be a standard and effective way to do this, as it is obviously very widely used on many websites.

+4
source share
3 answers

Unfortunately, the zip code distance calculator is not included in the .NET environment. Here are some ways to get around this.

Calculating the distance between two Zip codes in C #

API DISTANCE WIZARD

+7
source

I do not know that there is a "standard" way.

There may be existing services that can do this, and there may be databases that you can purchase or get for free that have usable data, but depending on your definition, it can be easy to calculate.

Think about it: your question can be interpreted as the shortest distance between two points (cheating using a point in the city lying in this zip code), or it can be interpreted as the distance between the edge of the two borders of the zip code. The first calculation is quite simple, but not 100% accurate. The second is not a simple problem. Finding the shortest distance is one of the subtasks, and finding the nearest points on the edge of irregular borders is another subtask (or searching for the nearest cities / known coordinates of the person calling this function).

So, without any information about the real problem that you are trying to solve, I can only assume that you do not care how accurate your data is. In this case, I propose to obtain a database of the coordinates of the cities in which they lie, and use standard subtractions / square root to determine the distance (Pythagorean theorem).

If you don’t care how accurate your data is, I suggest you do a research to find an existing database or service that has more accurate information that was previously calculated for you or processed by you.

+4
source

I wrote a PHP script back to calculate the distance between zip codes. Since then, due to the large number of user reviews, it has been modified so that the distance calculation is actually performed using SQL queries, as this was the most efficient way to do this.

You need a table with zip codes and their lat / long values. There are free and commercial databases with this information available all over the Internet.

Take a look at this PHP source code and look at the calcDistanceSql() method. It should be simple enough to keep track of the query that was built and implemented in SQL Server.

0
source

Source: https://habr.com/ru/post/1338686/


All Articles