Calculate zipcodes in range
Asked Answered
F

4

0

I have a website where users start by entering their zipcode.

I have a table with different shops which also contain the zip codes belonging to the shops.

Is it possible to get the zip codes within let's say a 5 km radius of the zip entered by the user?

So I can query the database and find all the shops near the user.

Can I use Google Maps for this? Should I get my hands on a pre-computed list of all zipcodes (single country) and their coordinates? Any other suggestions?

Furfuraceous answered 30/7, 2011 at 18:9 Comment(0)
S
1

You'll need that latitude and longitude of each zipcode, and then use the Haversine Formula to get the approximate distance.

Satterlee answered 30/7, 2011 at 18:24 Comment(0)
C
2

Google has a tutorial on this that should point you in the right direction. The bit of trigonometry in the SQL query is the only tough part. At first glance, there are basically two ways to go about this:

  1. Get the lat/long coordinates for each store. You can do this using Google Maps' Geocoding API by providing the street address for the store. You'd only need to do this once for each location.
  2. Only having the zip code for each store, and using a table of zip codes with geometrically-centered lat/long coordinates for each. Find the zip codes that are within range and then show the stores with those zip codes.

The first would be more accurate, but either works. If you're calculating distance from a zip code rather than an address, you'd still need to look up the lat/long coordinates for that zip code.

Confectionery answered 30/7, 2011 at 18:15 Comment(0)
K
2

you can store longitude/latitude for each zipcode. That data is available for free on the net , one such is here http://www.boutell.com/zipcodes/. Then you would find the zipcodes in the radius by using great circle distance function which is discussed here MySQL Great Circle Distance (Haversine formula)

Kinsfolk answered 30/7, 2011 at 18:25 Comment(2)
the last time I checked there were a couple of other countries on the world map...El
Yes, but there's only one country that uses ZIP codes. If you're looking for stores in an X mile radius, you're probably not interested in crossing any borders anyway. :)Confectionery
I
1

you need to get latitude/longtitude from the zipcode and then you can find places near by via maps or geoplaces api. head over to google maps api or YQL geo api.

Improvvisatore answered 30/7, 2011 at 18:21 Comment(0)
S
1

You'll need that latitude and longitude of each zipcode, and then use the Haversine Formula to get the approximate distance.

Satterlee answered 30/7, 2011 at 18:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.