mapboxgl.Marker
are implemented as simple HTML <div>
elements.
(mapboxgl.Marker
被实现为简单的HTML <div>
元素。)
You can attach standard event listener to them and toggle the popup manually:(您可以将标准事件侦听器附加到它们,并手动切换弹出窗口:)
const marker = new mapboxgl.Marker({/* options */});
const markerDiv = marker.getElement();
markerDiv.addEventListener('mouseenter', () => marker.togglePopup());
markerDiv.addEventListener('mouseleave', () => marker.togglePopup());
See docs: https://docs.mapbox.com/mapbox-gl-js/api/#marker#getelement
(参见文档: https : //docs.mapbox.com/mapbox-gl-js/api/#marker#getelement)
EDIT: Preventing the popup from opening on click
(编辑:防止单击打开弹出窗口)
I tried did some testing, and the only thing reliably working is to call marker.togglePopup()
in your own click handler
(我尝试进行了一些测试,唯一可靠的工作是在您自己的点击处理程序中调用marker.togglePopup()
)
map.on('click', event => {
const target = event.originalEvent.target;
const markerWasClicked = markerDiv.contains(target);
marker.togglePopup();
});
Full example: https://jsfiddle.net/am2jwtzg/
(完整示例: https : //jsfiddle.net/am2jwtzg/)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…