views:

425

answers:

4

I need a "find nearest location" on our website.

Where visitor enters their zip/postal code, then they are redirected to specific webpage for our nearest location. We have forty USA and Canada locations.

How can I build something like this? Could I do this with the Google Maps API? I already have a custom map on Google Maps. It's plotted with our locations. It would be nice to send Google Maps a command to say "what's our nearest location at __ zip code".

Any suggestions?

+3  A: 

Check out the proximity search featured here:

Using PHP/MySQL with Google Maps

You would have to geocode your zip codes into coordinates (also possible with the Google Maps API, but probably preferable to do with a full address), then you can run the proximimity search detailed in the tutorial.

Pekka
@Pekka Using the Google Maps API - is it possible to perform the calculation, get the results, but not display a map?
codemonkey613
@codemonkey yes it is. You can make a geocoding request (up to 10,000 per day) to the API without displaying it on a map, *but* I think their terms of service forbid any long-time storing of the geocoding results - they want you to make a request every time you need the data.
Pekka
@Pekka Thanks. 10,000 should be more than enough for our needs. And yeah, we won't be storing the results.
codemonkey613
+1  A: 

Hi,

Google Maps have restrictions in using is for business purposes, so you might want to consider those (restrictions) upfront. From my former experience I would suggest MapQuest API. You can find details here: http://developer.mapquest.com/ and some quick start guide here http://www.mapquest.com/features/developer_tools_oapi_quickstart

MapQuest seams to be better when it comes to commercial deals. Anyway, check their T&C before you will implement it in production.

They have JavaScriopt an I think regular WS for geo-location decoding.

Regards Konrad

Konrad
@Konrad I'm always for suggesting alternatives to Google Maps but Google's terms of service are very lenient also towards use in commercial sites. It's just sites that *charge for access* or are otherwise closed to the public where it is forbidden to use Google Maps AFAIK.
Pekka
@Pekka That sounds about right.
codemonkey613
Thanks for sharing this link though, their services look very interesting.
Pekka
Konrad
+2  A: 

if you have the longitudes and latitudes of each zipcode (search google) you can use the Haversine Formula to calculate nearest neighbours.

http://www.codecodex.com/wiki/Calculate_Distance_Between_Two_Points_on_a_Globe

f00
That would require me to find a USA/Canada database of zip/postal codes. Which would be over 80,000 records; and it would have to be up to date. Which seems a little overkill for such a simple application. So I'm hoping I can do this with Google Maps API.
codemonkey613
so a user inputs a zipcode, you select all 40+ of your locations as you dont know which is closest yet. you loop the locations in your app layer and call googlemaps api for each to calculate it's distance relative to the zip the user entered. 40+ ajax calls later you have all of the distances for all 40+ locations so you filter the list accordingly and display to user.hmmm, how responsive does this need to be ?
f00
It would be more like: user enters zip code, Google Maps API returns the lat and long for that zip code. I already have the lat and long for our 40+ locations, use Haversine Formula to calculate nearest.
codemonkey613
+2  A: 

You need a database of zip codes with longitude and latitude, from which you can calculate the distance.

mikerobi
The linked database uses 10 year old data. New ZIP codes are added very frequently.
Eric J.