Skip to content
Advertisement

Use google-map-react fitbounds useRef -> mapRef.current.fitBounds is not a function

I use google-map-react and I want to have the correct zoom and center to the map. To center the map I use the props center on GoogleMapReact component that I can calculate. But for the zoom it is more complex.

I could see the use of fitBounds but that is not available on “google-map-react”

Is there a way to calculate the zoom so that it contains the markers that I have defined. Or a function that does it by itself like fitbounds ?

Be careful not to confuse “google-map-react” (used here) with “react-google-map” (not used)

JavaScript

Advertisement

Answer

fitBounds does exist in google-map-react, but it’s a separate utility function rather than a function on the instance. There are examples of how to use it in the api. According to the source code, fitBounds is defined to take corner boundaries as the first parameter, and a height/width dictionary as the second:

JavaScript

and it returns a new object with newBounds, presumably to be destructured and used to set the new center and zoom.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement