Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps v3 fitBounds() Zoom too close for single marker

Is there a way to set a max zoom level for fitBounds()? My problem is that when the map is only fed one location, it zooms in as far as it can go, which really takes the map out of context and renders it useless. Perhaps I am taking the wrong approach?

Thanks in advance!

like image 228
Codey W Avatar asked Jul 26 '10 12:07

Codey W


People also ask

How do I change the zoom level on Google Maps?

Users can zoom the map by clicking the zoom controls. They can also zoom and pan by using two-finger movements on the map for touchscreen devices.

How do I fix zoom on Google Maps?

To fix Google maps zooming problems, for Google maps default zoom you want to know how to change the zoom level on Google Maps. You can change the zoom level by going to the Edit map page and then selecting 'default zoom level' in the map information section and then clicking save map.

What is zoom level in Google map?

The Google Maps API provides map tiles at various zoom levels for map type imagery. Most roadmap imagery is available from zoom levels 0 to 18, for example. Satellite imagery varies more widely as this imagery is not generated, but directly photographed.


1 Answers

I like mrt's solution (especially when you don't know how many points you will be mapping or adjusting for), except it throws the marker off so that it isn't in the center of the map anymore. I simply extended it by an additional point subtracting .01 from the lat and lng as well, so it keeps the marker in the center. Works great, thanks mrt!

// Pan & Zoom map to show all markers function fitToMarkers(markers) {      var bounds = new google.maps.LatLngBounds();      // Create bounds from markers     for( var index in markers ) {         var latlng = markers[index].getPosition();         bounds.extend(latlng);     }      // Don't zoom in too far on only one marker     if (bounds.getNorthEast().equals(bounds.getSouthWest())) {        var extendPoint1 = new google.maps.LatLng(bounds.getNorthEast().lat() + 0.01, bounds.getNorthEast().lng() + 0.01);        var extendPoint2 = new google.maps.LatLng(bounds.getNorthEast().lat() - 0.01, bounds.getNorthEast().lng() - 0.01);        bounds.extend(extendPoint1);        bounds.extend(extendPoint2);     }      map.fitBounds(bounds);      // Adjusting zoom here doesn't work :/  } 
like image 58
Ryan Avatar answered Sep 29 '22 21:09

Ryan