How to calculate the distance/radius on zipcodes?

Suppose I have all zip codes database, how may I allow my users search
all things within xxx miles from his/her zip codes? I have no idea on
the calculation of the math. anyone may help me with same codes? Thanks
in advance.

Surfman J. wrote:

Suppose I have all zip codes database, how may I allow my users search
all things within xxx miles from his/her zip codes? I have no idea on
the calculation of the math. anyone may help me with same codes? Thanks
in advance.
a book named
RailsSpace: Building a Social Networking Website with Ruby on Rails
have a chapter dealing with the same problem.

On 9/12/07, Surfman J. [email protected] wrote:

Suppose I have all zip codes database, how may I allow my users search
all things within xxx miles from his/her zip codes? I have no idea on
the calculation of the math. anyone may help me with same codes? Thanks
in advance.

You’d probably be best off looking at one of the geocoding plugins
available
for rails.

GeoKit, acts_as_geocode and acts_as_locateable all provide this
functionality.

James.


James S. - Web D.
Work : http://jystewart.net
Play : http://james.anthropiccollective.org

Surfman J. wrote:

Suppose I have all zip codes database, how may I allow my users search
all things within xxx miles from his/her zip codes? I have no idea on
the calculation of the math. anyone may help me with same codes? Thanks
in advance.

As I recall you can use Pythagoras (is this correct spelling in
English?) for this purpose. Let me know, I might be able to find the
query I used for this a while ago. At the moment I am also using a gem
for this: GeoRuby.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

You can find usefull information on Wikipedia, the free encyclopedia
Haversine_formula.

Hope that helps.

JP Antunes

On Sep 12, 2007, at 1:41 PM, Daniel Owen van Dommelen wrote:

English?) for this purpose. Let me know, I might be able to find the
query I used for this a while ago. At the moment I am also using a gem
for this: GeoRuby.


Posted via http://www.ruby-forum.com/.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (Darwin)

iD8DBQFG6CAA8Q7LSJzhNq4RAiSSAJ9rt6Yu0wUEiqOio3KlZAi2x1yQrACbB6Bc
Ky0T98x6I4dPJ2KGBf3iG2k=
=qTYF
-----END PGP SIGNATURE-----

Surfman J. wrote:

Suppose I have all zip codes database, how may I allow my users search
all things within xxx miles from his/her zip codes? I have no idea on
the calculation of the math. anyone may help me with same codes? Thanks
in advance.

Here’s a calculation that selects all zip codes within 50 miles (80.465
kilometers) from zip 01516 (longitude: -71.70485213724869, latitude:
42.04027444338595)

http://pastie.caboo.se/97232

Things to note:
1.) The zipcode table has three columns: zc_zip, the column for the
5-char zip code; zc_lat, the latitude of the zip code; zc_lon, for the
longitude.

2.) The query returns a list of zip codes within the specified radius

3.) The value 80.465 at the end of the query is the distance in
kilometers that you are searching. (To convert US miles to kilometers
multiply by 1.6093, i.e. 50 mi * 1.6093 = 80.465 km)

4.) I don’t claim to understand it, I just know that it works

http://pastie.caboo.se/97232

You can get a list of all US Zip codes with lat/lon values from
http://www.census.gov/geo/ZCTA/zcta.html. However, since this works on
lat/lon data it is not limited to just US zip codes. If you know the
starting lat/lon you can search any other lat/lon pairs in your DB.