将react-google-maps与 MarkerClusterer 一起使用,我如何在安装地图后从地图/集群中添加和删除标记,例如。当一个按钮被点击或在 onMarkerClustererClick 中?
我试过简单地制作一个新的标记,地图设置为refs.map
onMarkerClustererClick: () => (markerClusterer) => {
const marker = new Marker({
position: {lat: 56.565123, lng: 9.030908},
map: refs.map,
title: 'Hello World!'
});
},
以及使用在 react-google-maps 中用于 MarkerClusterer 的 markerclustererplus 中定义的.addMarker()函数,但没有任何效果。
onMarkerClustererClick: () => (markerClusterer) => {
const marker = new Marker({
position: {lat: 56.565123, lng: 9.030908},
title: 'Hello World!'
});
refs.markerClusterer.addMarker(marker);
},
两者都返回错误Uncaught TypeError: Cannot read property '__SECRET_MARKER_CLUSTERER_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
。
基于示例的代码来自: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>
);