reactjs - 如何在 react-google-map 中删除所有标记而没有任何副作用?
问题描述
我正在使用 react-google-maps 组件。
因为我有很多标记要显示在谷歌地图上,所以我使用了 MarkerClusterer。
我可以选择或取消选择 ag-grid 表中的行以使标记显示在 react-google-map 上。
如果我全选,所有标记都包含在checkedIPs 中,如果我取消全选,所有标记都会从checkedIPs 中删除。
MapWithMarkers 组件仅在已检查 IP 中显示标记。
现在,添加标记可以正常工作,但删除标记(从 ag-grid 表中取消选择)并没有像我预期的那样工作。
这需要很长时间。
请帮我解决这个问题。代码如下。
const MapWithMarkers = compose(
withStateHandlers(
() => ({
isOpen: -1
}),
{
onToggleOpen: ({ isOpen }) => index => {
return {
isOpen: isOpen === index ? -1 : index
};
},
handleMapClick: () => () => {
return {
isOpen: -1
};
},
onMarkerClustererClick: () => markerClusterer => {}
}
),
withScriptjs,
withGoogleMap
)(props => {
return (
<GoogleMap
defaultZoom={props.zoomLevel}
defaultCenter={props.center}
onClick={props.handleMapClick}
>
{props.checkedIPs.length !== 0 ? (
<MarkerClusterer
onClick={props.onMarkerClustererClick}
enableRetinaIcons
gridSize={50}
>
{props.cache.map((location, index) => {
if (props.checkedIPs.includes(location.ipAddress)) {
return (
<Marker
key={index}
position={{ lat: location.latitude, lng: location.longitude }}
onClick={() => props.onToggleOpen(index)}
>
{props.isOpen === index && (
<InfoWindow
position={{
lat: location.latitude,
lng: location.longitude
}}
onCloseClick={() => props.onToggleOpen(index)}
>
<div>
<h6>IP Address: {location.ipAddress}</h6>
</div>
</InfoWindow>
)}
</Marker>
);
}
})}
</MarkerClusterer>
) : (
<MarkerClusterer />
)}
</GoogleMap>
);
});
export default class MapContainer extends Component {
constructor(props) {
super(props);
this.state = {
zoomLevel: 2,
center: { lat: 14.397, lng: 160.644 },
markers: [],
checkedIPs: [],
isOpen: -1
};
}
componentWillReceiveProps(nextProps) {
const { cache, checkedIPs } = nextProps;
this.setState({
markers: cache,
checkedIPs: checkedIPs
});
}
render() {
return (
<MapWithMarkers
googleMapURL="..."
loadingElement={<div style={{ height: `100%` }} />}
containerElement={<div style={{ height: `100%`, width: `100%` }} />}
mapElement={<div style={{ height: `100%` }} />}
cache={this.state.markers}
checkedIPs={this.state.checkedIPs}
zoomLevel={this.state.zoomLevel}
center={this.state.center}
/>
);
}
}
`````````
All markers should be removed very fast without any effect,
but currently, as I navigate all markers and check if it is contained in checkedIPs, it stops for a while and then disappear.
解决方案
推荐阅读
- java - 无法引用变量 - 找不到符号
- python-3.x - NOT NULL 约束失败:/jobs/add/ 处的jobs_job.created_by_id IntegrityError
- input - 如何在 LispWorks 中正确询问用户的输入?
- android - 在 recyclerview kotlin 中加载更多内容时如何不重写项目?
- javascript - Webpack 无法在已经工作的节点包中编译 js
- java - 检测到的 JDK 版本:15.0.2 不在允许范围内 [1.8.0-101,1.8.9999]
- python - 在等待响应 Flask Python 时防止重复提交表单
- javascript - 为什么我会出错?TypeError:“无法读取未定义的属性‘执行’”
- swift - 斯威夫特:当您在文本字段中键入内容时,您将如何过滤数组下拉列表?
- reactjs - TypeError: n.toLowerCase 在使用 JSX 时不是函数