如何在使用MEAME功能时禁用MAPBOX点击事件?

发布于 2025-02-01 02:18:55 字数 1050 浏览 4 评论 0 原文

因此,我已经阅读了有关使用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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文