I'm using the google-maps-react library to add a google map with markers that update on state change.
I want to dynamically add markers after fetching locations from Google's place api and storing them in state. I fetch the data, add it to state then call displayMarkers:
displayMarkers = () => {
this.state.newStations.map((station, index) => {
let lat = station.geometry.location.lat();
let lng = station.geometry.location.lng();
return (
<Marker
key={index}
id={index}
position={{
lat: lat,
lng: lng,
}}
onClick={() => console.log("You clicked me!")}
/>
);
});
};
State is being updated but the markers do not appear on the map.
Reading the doc for google-maps-react, it seems to me that marker must be a child of map in order to be overlaid onto the map.
Marker To place a marker on the Map, include it as a child of the component.
Is there any way to return the Marker as a child of Map?
In the google maps API it seems as though you can do this like so:
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: 'Hello World!'
});
You pass in the value map, which is the map you wish your marker to be attached or overlaid onto. The prop "map" exists in google-maps-react but there doesn't seem to be a property in Marker which accepts map.