Categories
JavaScript Answers

How to use setZoom() after use fitBounds() with Google Maps API V3 with JavaScript?

Spread the love

Sometimes, we want to use setZoom() after use fitBounds() with Google Maps API V3 with JavaScript.

In this article, we’ll look at how to use setZoom() after use fitBounds() with Google Maps API V3 with JavaScript.

How to use setZoom() after use fitBounds() with Google Maps API V3 with JavaScript?

To use setZoom() after use fitBounds() with Google Maps API V3 with JavaScript, we can use the getZoom and setZoom methods.

For instance, we write

google.maps.event.addListenerOnce(map, "bounds_changed", function (event) {
  if (this.getZoom() > 15) {
    this.setZoom(15);
  }
});

to listen to the bounds_change event on the map with addListenerOnce.

In the event listener, we call getZoom to check of the zoom level is bigger than 15.

If it is, then we call setZoom to set the zoom level to 15.

Conclusion

To use setZoom() after use fitBounds() with Google Maps API V3 with JavaScript, we can use the getZoom and setZoom methods.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.