Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Can you auto-zoom in to nearest five results on embedded Google Maps?

10:42 am on Sep 23, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 25, 2003
votes: 0

If you have dozens of offices nationwide and you have Google maps embedded on your site showing them all. Can you set it to auto-zoom to nearest five offices to the postcode that has been entered?

Right now, when you type in a postcode and search, it seems to randomly choose what to show e.g. 200 mile radius with 15 offices showing, or a 20 mile radius with 2 offices showing. I can't make sense of it.

I just want it to always show the nearest 5 offices within the map.

2:21 pm on Sept 23, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 15, 2003
votes: 21

If you can determine which offices you would most want to have shown based on the postcode, you could try using the latLang coordinates of those offices to determine a midpoint for your map using a reasonable zoom factor. I say this knowing nothing whatsoever about how you'd actually perform the calculations with precision, but that's where I'd start. And I'd bet you could get away with simply taking the average of all of the latitudes and longitudes of the offices and get an acceptable value. Users are accustomed enough in using interactive maps to do their own zooming, so I'd expect you could come up with something acceptable without having to become a modern-day Vasco da Gama.
3:51 pm on Sept 23, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member Top Contributors Of The Month

joined:July 19, 2013
votes: 0

You'll need to calculate the proximity of the locations via PHP or JS and then use JavaScript to set the map. There are a bunch of proximity calculations around the 'net, so that part is fairly simple -- Just grab one you like, calculate*, sort and add the first 5 to the JavaScript for the map using the following: LatLngBounds(), bounds.extend() and map.fitBounds() in the gMaps v3 docs.

* I do recommend pre-calculating and storing the results rather than making visitors wait while you calculate on-the-fly
7:48 am on Sept 29, 2013 (gmt 0)

Preferred Member

5+ Year Member

joined:Aug 18, 2008
votes: 0

You can use google.maps.geometry.spherical.computeDistanceBetween function to determine distance between two google.maps.LatLng objects. Requires a "libraries=geometry" parameter on the url to the google maps script. So what you need is to have all the locations of your offices pre-geocoded and stored with their latitudes and longitudes. You then can geocode the address (in this case just a zipcode will suffice as well) entered by the user, and then store distances between user and all locations. Then sort the locations by distance low-to-high. Then it's just a matter of displaying the first five in the array of locations, and using google.maps.LatLngBounds to adjust the viewing area to contain those five locations. Here is an example fiddle: [jsfiddle.net...]