Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zip code distance calculator

I have a spreadsheet of addresses and I need to calculate the distance between all of their zip codes and my zip code. I'm fairly flexible on the method used, but I'm hoping for some sort of webservice or mathematic algorithm. US addresses only. Basically I need to feed in 2 zip codes and get out the distance between them.

I'm willing to use Excel formulas or VBA, and I can even code something in C#.net if needed.

How would you go about calculating these distances?

like image 755
MAW74656 Avatar asked Jul 07 '11 15:07

MAW74656


People also ask

How do I find the distance between two zip codes?

The Latitude and Longitude are needed to calculate the distance between two locations with following formula: =acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos(lon2-lon1))*6371 (Ps: 6371 is Earth radius in km.)

How many miles does a zip code cover?

The average land area of a zip code is around 90 square miles, which is similar to the size of Knoxville, Tennessee. Much like the land area, there are vast differences in population size.


2 Answers

You could use Latitude and Longitude.

Excel:

=IF(SIN(Lat1) * SIN(Lat2) + COS(Lat1) * COS(Lat2) * COS(Long1 - Long2) > 1, 
RadiusofEarth * ACOS(1), RadiusofEarth * 
ACOS(SIN(Lat1) * SIN(Lat2) + COS(Lat1) * COS(Lat2) * COS(Long1-Long2)))

VB.net imports System.Math

Private Function Distance(ByVal lat1 As Double, ByVal lon1 As Double, ByVal lat2 As Double, ByVal lon2 As Double, ByVal unit As String) As Double
        Dim theta As Double = lon1 - lon2
        Dim dist = System.Math.Sin(deg2rad(lat1)) * System.Math.Sin(deg2rad(lat2)) + System.Math.Cos(deg2rad(lat1)) * System.Math.Cos(deg2rad(lat2)) * System.Math.Cos(deg2rad(theta))
        dist = System.Math.Acos(dist)
        dist = rad2deg(dist)
        dist = dist * 60 * 1.1515
        Select Case unit
            Case "K"
                dist = dist * 1.609344
            Case "N"
                dist = dist * 0.8684
        End Select

        Return dist

    End Function

Other Useful Links(First one also mentions VBA alternatives)

ExcelLatLong (Also mentions VBA alternatives)

Zips by Lat Long Lookup

VBA discussion

EDIT: Link added due to comment discussion

More Info(Excel Formula)

like image 120
sealz Avatar answered Oct 06 '22 07:10

sealz


It's pretty simple actually. Download a database of zip codes' GPS coordinates, there's plenty of sites that have this data available for download. They would list the coordinates for the center of the zip code.

Use a formula to calculate shortest distance: (ex: http://www.movable-type.co.uk/scripts/latlong.html)

like image 38
MichaelP Avatar answered Oct 06 '22 06:10

MichaelP