如何在mapbox gl中以编程方式关闭所有弹出窗口? [英] How to close all popups programmatically in mapbox gl?
本文介绍了如何在mapbox gl中以编程方式关闭所有弹出窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以,我知道Mapbox GL Api中有 Marker.togglePopup()
.但是我们可以通过编程方式关闭所有弹出窗口吗?
So, I know that we have Marker.togglePopup()
in Mapbox GL Api.
But Can we close all popups programmatically ?
推荐答案
以下是示例: https://jsfiddle.net/kmandov/eozdazdr/
单击右上角的按钮以打开/关闭弹出窗口.
Here is an example: https://jsfiddle.net/kmandov/eozdazdr/
Click the buttons at the top right to open/close the popup.
鉴于您有一个弹出窗口和一个标记:
Given you have a popup and a marker:
var popup = new mapboxgl.Popup({offset:[0, -30]})
.setText('Construction on the Washington Monument began in 1848.');
new mapboxgl.Marker(el, {offset:[-25, -25]})
.setLngLat(monument)
.setPopup(popup)
.addTo(map);
您可以通过以下方式关闭弹出窗口:
You can close the popup by calling:
popup.remove();
或者您可以通过以下方式打开它:
or you can open it by calling:
popup.addTo(map);
您可以在标记源中看到, togglePopup
在内部使用以下两种方法:
As you can see in the Marker source, togglePopup
uses these two methods internally:
togglePopup() {
var popup = this._popup;
if (!popup) return;
else if (popup.isOpen()) popup.remove();
else popup.addTo(this._map);
}
这篇关于如何在mapbox gl中以编程方式关闭所有弹出窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文