Cannot read property 'setMap' of undefined
See original GitHub issueI 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?
Issue Analytics
- State:
- Created 7 years ago
- Comments:29 (2 by maintainers)
Top Results From Across the Web
Cannot read setMap property of undefined while using Google ...
Cannot read setMap property of undefined. which points to the addMarker and deleteMarker functions. var map; var marker; var allmarkers ...
Read more >Uncaught TypeError: Cannot read property 'setMap' of undefined
Uncaught TypeError : Cannot read property 'marker' of undefined. my code is. var Chennai = new google.maps.LatLng(13.0827,80.2707);
Read more >google maps api - clear existing markers before adding more
setMap(map); }. I get this error in Chrome though: Uncaught TypeError: Cannot read property 'setMap' of undefined.
Read more >Unable to Get property 'setMap' of undefined or null reference
some of the error are related with this. If this test component cannot show pin address in map we get the error. Also,...
Read more >Google Maps JavaScript API V3 Reference
Properties : AutomaticViewportMode , FleetEngineDeliveryFleetLocationProvider ... VisualizationLibrary interface. Properties: HeatmapLayer ... Methods: set.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Right. Those lines are there. The lines that should be there are these:
That’s the problem.
Are there any updates on whether this is getting fixed?