Categories
JavaScript Answers

How to fix getBounds is undefined with Google Maps API v3 and JavaScript?

Spread the love

Sometimes, we want to fix getBounds is undefined with Google Maps API v3 and JavaScript.

In this article, we’ll look at how to fix getBounds is undefined with Google Maps API v3 and JavaScript.

How to fix getBounds is undefined with Google Maps API v3 and JavaScript?

To fix getBounds is undefined with Google Maps API v3 and JavaScript, we can use the map.

For instance, we write

const map = new google.maps.Map(document.getElementById("map"), {
  zoom: 12,
  center: new google.maps.LatLng(55.755327, 37.622166),
  mapTypeId: google.maps.MapTypeId.ROADMAP,
});

google.maps.event.addListener(map, "bounds_changed", () => {
  console.log(map.getBounds());
});

to create a map with the google.maps.Map constructor.

We set the zoom, center, and the map type by setting mapTypeId.

Then we call addListener to listen for the bounds_changed event on the map.

And we get the bounds with map.getBounds in the bounds_changed event listener.

Conclusion

To fix getBounds is undefined with Google Maps API v3 and JavaScript, we can use the map.

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.