Categories
JavaScript Answers

How to call Google maps API V3 fitBounds method with LatLngBounds with JavaScript?

Spread the love

Sometimes, we want to call Google maps API V3 fitBounds method with LatLngBounds with JavaScript.

In this article, we’ll look at how to call Google maps API V3 fitBounds method with LatLngBounds with JavaScript.

How to call Google maps API V3 fitBounds method with LatLngBounds with JavaScript?

To call Google maps API V3 fitBounds method with LatLngBounds with JavaScript, we call extend with the SW and NE points.

For instance, we write

const bounds = new google.maps.LatLngBounds();
bounds.extend(myPlace);
bounds.extend(item1);
map.fitBounds(bounds);

to create the LatLngBounds bounds object.

Then we call extend with the myPlace and item1 points object with the SW and NE bounds.

And then we call map.fitBounds with the bounds.

Conclusion

To call Google maps API V3 fitBounds method with LatLngBounds with JavaScript, we call extend with the SW and NE points.

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 *