I'm trying to push array of markers after clicking on map. I've tried this (under "this doesn't work") but it raise an error.
PS: how can I get location of my cursor to create new marker depending on cursor position?
I'm really stuck. Thanks for any advice.
import React, { Component } from 'react';
import { withGoogleMap, GoogleMap, Marker } from 'react-google-maps';
class Map extends Component {
state = {
markers: [
{position: {lat:49.79, lng: 18}, key:1},
{position: {lat:49.69, lng: 18}, key:2},
//{position: {lat:49.59, lng: 18}, key:3}
]
};
//this doesn't work
handleMapClick() {
this.setState({ markers: [...this.state.markers, {position: {lat:49.59,
lng: 18}, key:3}]})
}
render() {
const GoogleMapExample = withGoogleMap(props => (
<GoogleMap
defaultCenter = { { lat: 49.82, lng: 18.16 } }
defaultZoom = { 10 }
onClick = {this.handleMapClick}
>
{this.state.markers.map(marker => <Marker position=
{marker.position} key={marker.key} />)}
</GoogleMap>
));
return(
<div>
<GoogleMapExample
containerElement={ <div style={{ height: `600px`, width:
'800px',display: "inline-block" }} /> }
mapElement={ <div style={{ height: `100%` }} /> }
/>
</div>
);}};
export default Map;