Fitbounds with ref error.
See original GitHub issueHi there, how do we do a fitbounds of all the markers in google-map-react? Any example for that?
This is what I done but with error:
const PGoogleMap = compose( withProps({ googleMapURL:
https://maps.googleapis.com/maps/api/js?key=${GOOGLE_MAP_API_KEY}&v=3.exp&libraries=geometry,drawing,places`,
loadingElement: <div style={{ height: 100%
}} />,
containerElement: <div style={{ height: 400px
}} />,
mapElement: <div style={{ height: 100%
}} />
}),
withHandlers(() => {
const refs = {
map: undefined,
}
return {
onSetFit: () => ref => {
refs.map = ref
var mBounds = new google.maps.LatLngBounds();
mCoords.forEach((p) => {
var latLng = new google.maps.LatLng(p.lat, p.lng);
mBounds.extend(latLng);
})
ref.map.fitBounds(mBounds)
}
}
}),
withScriptjs,
withGoogleMap
)((props) =>
<GoogleMap
ref={props.onSetFit}
defaultZoom={18}
defaultCenter={props.centerFocus}
{props.patrol.map((marker, i) => ( <Marker key={marker.id} position={{ lat: marker.lat, lng: marker.lng }} > </Marker> ))} <Polyline path={props.coordinates} /> </GoogleMap> )`
The code is working fine when showing the map.
But when I turn to other page without the map, the map will still be mounted on the bottom of the page. Upon checking the console, this error appear ref.map is null. Warning: Exception thrown by hook while handling onSetChildren: Invariant Violation: Expected hook events to fire for the child before its parent includes it in onSetChildren().
Anybody knows how to fix it?
Issue Analytics
- State:
- Created 6 years ago
- Comments:9 (2 by maintainers)
@ahgan84 you can use fitBounds()
this is my code it’s working
@ahgan84 the #713 way. This way is the deprecated way