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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *