Google Map will not recenter when state / props changes #512
Copy link
Copy link
Closed
Description
Activity
@BenAhlander Try this:
const bindResizeListener = (map, maps, bounds) => { maps.event.addDomListenerOnce(map, 'idle', () => { maps.event.addDomListener(window, 'resize', () => { map.fitBounds(bounds); }); }); }; const apiIsLoaded = (map, maps) => { if (map) { const bounds = new maps.LatLngBounds(); map.fitBounds(bounds); bindResizeListener(map, maps, bounds); } }; const GoogleMap = props => ( <GoogleMapReact zoom={props.zoom} center={props.center} bootstrapURLKeys={{ key: [YOUR_KEY], language: 'en', }} yesIWantToUseGoogleMapApiInternals onGoogleApiLoaded={({ map, maps }) => apiIsLoaded(map, maps)} > {props.children} </GoogleMapReact> );Reacted by Ilja Reznik@BenAhlander Any luck? Please reopen issue if its still an issue.
I have the same problem, I get my data from a websocket connection, then I do this:
componentDidMount() { this.socket.on('my-data', myData => { this.setState({center: myData.center})/ }) } render() { return ( <MyMap center={this.state.center} > ); }I can show the new location but the map is still at the old location
@ivanquirino You're updating your state in componentDidMount but render is not going to be triggered , the problem is the way you're using react lifecycle. Please check #546 (comment)
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
- locked as resolved and limited conversation to collaborators
on Dec 1, 2019
Metadata
Metadata
Assignees
Labels
No labels
Here is a copy of my component
The div updates when this.props.center changes but center does not update.
I have tried using "onGoogleApiLoaded" with "yesIWantToUseGoogleMapApiInternals". I have tried to keep the dom elements from rendering until the updated center has been set on state.
Any help on this would be greatly appreciated. Thanks!