homepage Welcome to WebmasterWorld Guest from 54.197.211.197
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

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

WebmasterWorld Senior Member 10+ Year Member



 
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.

Thanks
Mike

 

rainborick

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4611977 posted 2:21 pm on Sep 23, 2013 (gmt 0)

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.

JD_Toims

WebmasterWorld Senior Member Top Contributors Of The Month



 
Msg#: 4611977 posted 3:51 pm on Sep 23, 2013 (gmt 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

astupidname

5+ Year Member



 
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...]

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved