Skip to content

Instantly share code, notes, and snippets.

@mbeaty
Created October 4, 2011 08:54
Show Gist options
  • Save mbeaty/1261182 to your computer and use it in GitHub Desktop.
Save mbeaty/1261182 to your computer and use it in GitHub Desktop.
Google Maps fit bounds on marker and re-set zoom level
// addMarker() is just a method that creates a marker and adds it to a marker array; it returns
// the marker it just added.
var bounds = new google.maps.LatLngBounds();
var markerPos = addMarker(point.latitude, point.longitude).getPosition()
bounds.extend(markerPos);
map.fitBounds(bounds);
map.setZoom(14);
@bhartendukumar12
Copy link

Is there a way to set the zoom level to each markers.If yes how can i set the zoom level to each markers?

@TNS911
Copy link

TNS911 commented May 7, 2021

Shouldnt work always because its done asynchronously..

Its better to re-set the zoom like that:

map.fitBounds(this.map.bounds);

zoomChangeBoundsListener = 
    google.maps.event.addListenerOnce(map, 'bounds_changed', function(event) {
        if ( this.getZoom() ){   // or set a minimum
            this.setZoom(16);  // set zoom here
        }
});

setTimeout(function(){google.maps.event.removeListener(zoomChangeBoundsListener)}, 2000);

Thanks u so much, i have solved this bug about zoom out map after load marker in 2hours. Thanks u !!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment