question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot read property 'setMap' of undefined

See original GitHub issue

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?

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:29 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
jsmapr1commented, Jan 5, 2017

Right. Those lines are there. The lines that should be there are these:

if (this.props.map !== prevProps.map || this.props.position !== prevProps.position) {
  if (this.marker) {
    this.marker.setMap(null);
  }
  this.renderMarker();
}

That’s the problem.

3reactions
nats12commented, Apr 25, 2017

Are there any updates on whether this is getting fixed?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found