在切换弹出窗口之前,MapboxGL 标记弹出元素不存在

发布于 2025-01-12 09:26:14 字数 1398 浏览 0 评论 0原文

在我目前正在开发的网络应用程序中,我用标记显示了城市广场周围的多个位置。当轻击/单击时,标记会显示一个弹出窗口,其中包含 4 项操作菜单列表 - 例如更多信息、查看图库等。在典型的城市广场上,我可能会显示 5 到 25 个这样的标记。

显示广场地图后,通过 JavaSript 创建标记。代码是这样的,

const element = document.createElement('div');
element.classList.add('popim000','popimNN');
//popimNN where NN is 01, 02 etc determines the map pin used

const marker = new mapboxgl.Marker({element}).setLngLat(entry.latlng),
      popup = new mapboxgl.Popup({offset:25}),
      html = '<span class='laSpan xpop'>Read More</span>' + 
             '<span class='laSpan xpop'>View Gallery</span>' + 
             '<span class='laSpan xpop'>Not of interest</span>';


marker.addTo(mapBoxMapInstance);
popup.setHTML(html);
marker.setPopup(popup);

为了可读性,我对实际代码进行了简化和格式化。我遇到的问题是这样的。我想检测上面跨度上的 mousedowntouchstart 事件,以便执行两件事

  1. 隐藏弹出窗口
  2. 显示另一个执行所需操作的 HTML 弹出窗口(不是 MapBox) - 显示 。

为了做到这一点,我想我可以通过发出 document.getElementsByClassName('xpop') 来简单地识别所有相关的跨度 然而,这不起作用——看起来 MapBox 实际上并没有将弹出 HTML 渲染到 DOM 中,直到它弹出为止。

我可以通过这样做来规避这个问题

  • 首先在每个标记生成时发出一个 marker.togglePopup() 在它被配置+附加到其父地图之后
  • 然后获取对每个 的引用当弹出窗口仍在显示时,>xpop 跨度。

这可行,但对我来说感觉像是一个非常黑客的解决方案。是否有另一种方法可以实现相同的最终结果 - 将弹出 HTML mousedowntouchstart 事件与我自己的事件处理程序链接?

In the webapp that I am currently developing I show a number of locations around a city square with markers. When tapped/clicked the markers display a popup with a 4 item menulist of actions - e.g. More Information, View Gallery etc. On a typical city square I may show between 5 and 25 such markers.

The markers are created from JavaSript after the map of the square has been displayed. The code goes something like this

const element = document.createElement('div');
element.classList.add('popim000','popimNN');
//popimNN where NN is 01, 02 etc determines the map pin used

const marker = new mapboxgl.Marker({element}).setLngLat(entry.latlng),
      popup = new mapboxgl.Popup({offset:25}),
      html = '<span class='laSpan xpop'>Read More</span>' + 
             '<span class='laSpan xpop'>View Gallery</span>' + 
             '<span class='laSpan xpop'>Not of interest</span>';


marker.addTo(mapBoxMapInstance);
popup.setHTML(html);
marker.setPopup(popup);

I have simplified and formatted the actual code in the interests of readability. The problem I have run into is this. I want to detect mousedown and touchstart events on the spans above in order to do two things

  1. Hide the popup
  2. Display another HTML popup (not MapBox) that performs the required action - showing more information, displaying a picture gallery etc.

In order to do this I thought I could simply identify all relevant spans by issuing a document.getElementsByClassName('xpop'). However, this does not work - it looks like MapBox does not actually render the popup HTML into the DOM until it has, well, popped up.

I can circumvent the problem by doing this

  • First issue a marker.togglePopup() on each marker as it is generated and after it has been configured + attached to its parent map
  • Then grab a reference to each xpop span while the popup is still on display.

This works but feels like a very hackish solution to me. Is there another way to accomplish the same end result - linking the popup HTML mousedown and touchstart events with my own event handlers?

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

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

发布评论

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