Msg#: 4611977 posted 10:42 am on Sep 23, 2013 (gmt 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.
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.
* I do recommend pre-calculating and storing the results rather than making visitors wait while you calculate on-the-fly
Msg#: 4611977 posted 7:48 am on Sep 29, 2013 (gmt 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...]