因此,我已经阅读了有关使用Mapbox关闭活动听众的信息。现在,我有当前的代码来单击时创建标记。
const onClickEventMarker = (event) => {
setClassName(style.radial);
props.marker.setPopup(
new Popup({
closeOnMove: true,
anchor: 'center',
className: style.container,
})
.addTo(map)
.setDOMContent(popupRef.current)
.setLngLat(event.lngLat.wrap())
);
props.marker.setLngLat(event.lngLat.wrap()).addTo(map);
}
map.on('click', onClickEventMarker);
这可以正常工作,并且可以按预期为地图添加标记。
在Mapbox中,您具有DRAW功能。
每当您单击地图时,这都会在地图上绘制一条线以测量距离。
现在,我已经设置了一个布尔值,何时进行测量进行检查,我希望在此期间关闭Onclicklickeventmarker。
useEffect(() => {
if(props.isMeasuring) {
map.off('click', onClickEventMarker);
} else {
map.on('click', onClickEventMarker);
}
}, [props.isMeasuring]);
预期的结果是点击事件关闭,但事实并非如此。我已经检查了代码是否真正进入关闭且效果,因此这不是问题。
有人知道解决方案还是我做错了什么?我检查了很多帖子,但似乎找不到解决方案。感谢帮助!
So I have read about turning off event listeners with Mapbox. Now I have the current code to create a marker on click.
const onClickEventMarker = (event) => {
setClassName(style.radial);
props.marker.setPopup(
new Popup({
closeOnMove: true,
anchor: 'center',
className: style.container,
})
.addTo(map)
.setDOMContent(popupRef.current)
.setLngLat(event.lngLat.wrap())
);
props.marker.setLngLat(event.lngLat.wrap()).addTo(map);
}
map.on('click', onClickEventMarker);
This works fine and it adds a marker to the map as expected.
In mapbox you have the draw functionality.
https://docs.mapbox.com/mapbox-gl-js/example/measure/
This will, whenever you clicked the map, draw a line on the map to measure a distance for example.
Now I have set a boolean to check whenever measuring is going on and I want the onClickEventMarker to turn off during this.
useEffect(() => {
if(props.isMeasuring) {
map.off('click', onClickEventMarker);
} else {
map.on('click', onClickEventMarker);
}
}, [props.isMeasuring]);
The expected result would be that the click event turns off, but it doesn't. I have already checked if the code actually goes into the off and on and it does, so that isn't the issue.
Does anyone know a solution to this or am I doing something wrong? I checked alot of posts but can't seem to find a solution. Appreciate the help!
发布评论