Using react-google-maps with MarkerClusterer, how can I add and remove markers from the map/clusterer after the map is mounted, eg. when a button is clicked or in onMarkerClustererClick?
I've tried simply making a new Marker with map set to refs.map
onMarkerClustererClick: () => (markerClusterer) => {
const marker = new Marker({
position: {lat: 56.565123, lng: 9.030908},
map: refs.map,
title: 'Hello World!'
});
},
as well as using the .addMarker() function defined in markerclustererplus used for MarkerClusterer in react-google-maps, but nothing works.
onMarkerClustererClick: () => (markerClusterer) => {
const marker = new Marker({
position: {lat: 56.565123, lng: 9.030908},
title: 'Hello World!'
});
refs.markerClusterer.addMarker(marker);
},
which both returns the error Uncaught TypeError: Cannot read property '__SECRET_MARKER_CLUSTERER_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
.
Code based on the example from: https://tomchentw.github.io/react-google-maps/#markerclusterer
const MapWithAMarkerClusterer = compose(
withProps({
googleMapURL: "https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=geometry,drawing,places&key=AIzaSyATVkXuYRZCkB73ZsPEJisDy74GnVusIJw",
loadingElement: <div style={{ height: `100%` }} />,
containerElement: <div className="mapContainer" />,
mapElement: <div style={{ height: `100%` }} />,
}),
withHandlers(() => {
const refs = {
map: undefined,
markerClusterer: undefined,
}
return {
onMapMounted: () => ref => {
refs.map = ref
},
onMarkerClustererMounted: () => ref => {
refs.markerClusterer = ref
},
onMarkerClustererClick: () => (markerClusterer) => {
const marker = new Marker({
position: {lat: 56.565123, lng: 9.030908},
title: 'Hello World!'
});
refs.markerClusterer.addMarker(marker);
},
}
}),
withScriptjs,
withGoogleMap
)(props =>
<GoogleMap
ref={props.onMapMounted}
defaultZoom={3}
defaultCenter={{ lat: 25.0391667, lng: 121.525 }}
>
<MarkerClusterer
ref={props.onMarkerClustererMounted}
onClick={props.onMarkerClustererClick}
averageCenter
enableRetinaIcons
gridSize={60}
>
{props.hotels.results.map((item, index) => (
<Marker
key={index}
position={{lat: parseFloat(item.latitude), lng: parseFloat(item.longitude) }}
/>
))}
</MarkerClusterer>
</GoogleMap>
);