Cannot read property 'setMap' of undefined
I am getting an error caused by Marker.js, using the google-maps-react downloaded from npm. The code I have is below:
import React from 'react'
import {InfoWindow, Marker, Map, GoogleApiWrapper} from 'google-maps-react'
class RegionalMap extends React.Component {
constructor(props){
super(props);
this.state = {
showingInfoWindow: false,
activeMarker: {},
selectedPlace: {},
};
}
onMarkerClick = (props, marker, e) => {
this.setState({
selectedPlace: props,
activeMarker: marker,
showingInfoWindow: true
});
};
onInfoWindowClose = () => {
this.setState({
showingInfoWindow: false,
activeMarker: null
});
};
onMapClicked = (props) => {
if (this.state.showingInfoWindow) {
this.setState({
showingInfoWindow: false,
activeMarker: null
});
}
};
render() {
return (
<div id="google-map-holder">
<Map google={this.props.google}
style={{width: '100%', height: '100%', position: 'inherit'}}
className={'map'}
zoom={14}
onClick={this.onMapClicked}>
<Marker
mapCenter="aarr"
onClick={this.onMarkerClick}
name={'SOMA'}
position={{lat: 37.778519, lng: -122.405640}} />
<Marker
onClick={this.onMarkerClick}
name={'Dolores park'}
position={{lat: 37.759703, lng: -122.428093}} />
<InfoWindow
marker={this.state.activeMarker}
visible={this.state.showingInfoWindow}
onClose={this.onInfoWindowClose}>
<div>
<h1>{this.state.selectedPlace.name}</h1>
</div>
</InfoWindow>
</Map>
</div>
)
}
}
export default GoogleApiWrapper({
apiKey: MY_API_KEY
})(RegionalMap);
I get an error in the componentDidUpdate function, caused by the line this.marker.setMap(null);. It appears that this error message is occurring because inside the renderMarker function, the google variable is undefined:
if (!google) {
return null;
}
The funny thing is that when I remove the Marker code from the render function, it works perfectly:
import React from 'react'
import {InfoWindow, Marker, Map, GoogleApiWrapper} from 'google-maps-react'
class RegionalMap extends React.Component {
constructor(props){
super(props);
this.state = {
showingInfoWindow: false,
activeMarker: {},
selectedPlace: {},
};
}
onMarkerClick = (props, marker, e) => {
this.setState({
selectedPlace: props,
activeMarker: marker,
showingInfoWindow: true
});
};
onInfoWindowClose = () => {
this.setState({
showingInfoWindow: false,
activeMarker: null
});
};
onMapClicked = (props) => {
if (this.state.showingInfoWindow) {
this.setState({
showingInfoWindow: false,
activeMarker: null
});
}
};
render() {
return (
<div id="google-map-holder">
<Map google={this.props.google}
style={{width: '100%', height: '100%', position: 'inherit'}}
className={'map'}
zoom={14}
onClick={this.onMapClicked}>
<InfoWindow
marker={this.state.activeMarker}
visible={this.state.showingInfoWindow}
onClose={this.onInfoWindowClose}>
<div>
<h1>{this.state.selectedPlace.name}</h1>
</div>
</InfoWindow>
</Map>
</div>
)
}
}
Any help?
to make a temporary workaround i made it to work like this.
import {InfoWindow, Map, GoogleApiWrapper} from 'google-maps-react' import Marker from '/imports/ui/components/googlemap/Marker'
copied the Marker component from the repo and paste it to your folder then call it as a single component..
just a temporary though while waiting for the npm package updates
^^^ I tried the above solution. It worked for me. Note: you also need to get (copy) the String.js file from lib and import it locally. Thanks!
I might try to look into fixing the npm package myself.
Are there any updates on whether this is getting fixed?
It is working sometimes (after many refreshes) but after refreshing, this error comes. Uncaught (in promise) TypeError: Cannot read property 'setMap' of undefined
@americool I tried doing what you mentioned.
However I am getting an error with regards to "define" being undefined. How do you get over this?
Any eta for the official fix? It's pretty annoying and I don't feel confident pushing the aforementionned quick fix to production once my project is ready to be published.
@njho Same thing here, can't even use this fix because of that. Were you able to make it work?
Honestly it's been a while since I worked with this lib, it felt somewhat abandoned last time I was on here. You might wanna try https://github.com/istarkov/google-map-react. It's what I used for my last project.
Can anyone tell me how can I handle the error when google maps API is not loading?