[AccessD] Zipcodes within a radius

MartyConnelly martyconnelly at shaw.ca
Thu Mar 22 21:20:13 CDT 2007


Well assuming you have something like a zip centroid with a lat/long
and an address with a lat/long, there are methods for calculating
distance between points on the surface of sphere of varying accuracy
If the calculated distance is less than radius then you are inside the 
circle.
Some methods are the simpler "spherical law of cosines",
Haversine and the more accurate ellipsoidal Vincenty formula.

http://www.movable-type.co.uk/scripts/LatLong.html
http://mathworld.wolfram.com/SphericalTrigonometry.html
http://www.movable-type.co.uk/scripts/LatLongVincenty.html

There is also the the Great Circle Arc method
it is available in NeatCode.mdb sample
Has a lot of useful trig functions
under MS Access 97 downloads somewhere
I have a copy if unfindable

Don't forget to translate lat/long degrees to Rads for Trig functions

'Circle Method Example
'has problems when crossing equator or greenwich meridian
'instead use Vincenty
'untested to accuracy and math but should be good to 10 metres

Const PI = 3.14159265359
Sub incircle()
'great-circle distances between the two points –
'that is, the shortest distance over the earth’s surface
'– using the ‘Haversine’ formula.
Dim R As Double
Dim Lat1 As Double
Dim Lat2 As Double
Dim long1 As Double
Dim long2 As Double
Dim d As Double
'convert decimal degrees to rads
Lat1 = 53.1 * PI / 180
long1 = 1.8 * PI / 180
Lat2 = 52.1 * PI / 180
long2 = 0.1 * PI / 180
'earths_radius = 3963 // number of miles in the radius
R = 3963
'earths_radius = 6371 // number of kilometres in the radius

'delta
Dim dlat As Double
Dim dlong As Double
Dim a As Double
Dim c As Double
dlat = Lat2 - Lat1
dlong = long2 - long1
a = Sin(dlat / 2) * Sin(dlat / 2) + _
Cos(Lat1) * Cos(Lat2) * Sin(dlong / 2) * Sin(dlong / 2)
'might use ATAN2 here for accuracy

c = 2 * Atn(Sqr(a) / Sqr(1 - a))
d = R * c
Debug.Print "distance in miles=" & d

End Sub

JWColby wrote:

>Does anyone know how to get the zip codes with a radius of an address?
> 
>John W. Colby
>Colby Consulting
>www.ColbyConsulting.com
> 
>  
>

-- 
Marty Connelly
Victoria, B.C.
Canada




More information about the AccessD mailing list