google-maps-react icon indicating copy to clipboard operation
google-maps-react copied to clipboard

Cannot read property 'setMap' of undefined

Open alexanderwhatley opened this issue 9 years ago • 29 comments

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?

alexanderwhatley avatar Jan 03 '17 00:01 alexanderwhatley

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

wiredots01 avatar Jan 16 '17 09:01 wiredots01

^^^ 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.

americool avatar Feb 03 '17 17:02 americool

Are there any updates on whether this is getting fixed?

nats12 avatar Apr 25 '17 08:04 nats12

It is working sometimes (after many refreshes) but after refreshing, this error comes. Uncaught (in promise) TypeError: Cannot read property 'setMap' of undefined

YashGulati avatar Apr 26 '17 06:04 YashGulati

@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?

njho avatar Jun 17 '17 01:06 njho

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.

loanlaux avatar Jul 14 '17 18:07 loanlaux

@njho Same thing here, can't even use this fix because of that. Were you able to make it work?

loanlaux avatar Jul 14 '17 18:07 loanlaux

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.

americool avatar Jul 14 '17 19:07 americool

Can anyone tell me how can I handle the error when google maps API is not loading?

abhi2399 avatar Dec 12 '18 14:12 abhi2399