问题
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?
回答1:
You'll need that latitude and longitude of each zipcode, and then use the Haversine Formula to get the approximate distance.
回答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)
回答3:
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:
- 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.
- 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.
回答4:
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.
来源:https://stackoverflow.com/questions/6885034/calculate-zipcodes-in-range