Compute zipcodes in a range

I have a website where users start entering their zip code.

I have a table with different stores, which also contains the postal codes belonging to the stores.

Is it possible to get the zip codes inside, let them say that the radius of the width is 5 km, entered by the user?

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

Can I use Google Maps for this? Should I get a pre-calculated list of all zipcodes (one country) and their coordinates? Any other suggestions?

0
source share
4 answers

You will need this latitude and longitude of each zipcode, and then use the Haversin Formula to get an approximate distance.

+1
source

Google has a tutorial that should point you in the right direction. The trigonometry bit in an SQL query is the only difficult part. At first glance, there are two ways to do this:

  • Get lat / long coordinates for each store. You can do this using the Google Maps Geocoding API by entering the street address for the store. You will only need to do this once for each location.
  • Only the availability of a zip code for each store and the use of a zip code table with geometrically centered lat / long coordinates for each. Find the zip codes within the range, and then show the stores with these zip codes.

The first will be more accurate, but either works. If you are calculating the distance from the zip code and not from the address, you still need to look for the lat / long coordinates for that zip code.

+2
source

You can store longitude / latitude for each zip code. This data is available for free online, for example, http://www.boutell.com/zipcodes/ . Then you will find zipcodes in radius using the large circle distance function discussed here Great distance between MySQL (Haversin formula)

+2
source

you need to get latitude / longitude from zipcode, and then you can find places nearby using maps or api geometries. go to google maps api or YQL geo api.

+1
source

All Articles