8

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?

codemonkey613
  • 960
  • 1
  • 16
  • 26

3 Answers3

4

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

Jon Black
  • 16,223
  • 5
  • 43
  • 42
  • 1
    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 Mar 24 '10 at 19:07
  • 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 ? – Jon Black Mar 24 '10 at 19:27
  • 1
    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 Mar 24 '10 at 22:17
3

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

mikerobi
  • 20,527
  • 5
  • 46
  • 42
0

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 Pawlus
  • 1,641
  • 3
  • 14
  • 20
  • 1
    @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 Mar 24 '10 at 18:53
  • Thanks for sharing this link though, their services look very interesting. – Pekka Mar 24 '10 at 19:12
  • @Pekka I agree with you, but I still have some doubts, what exactly is allowed or not in theirs T&C, that's why I would suggest MapQuest. – Konrad Pawlus Mar 24 '10 at 20:06