如何始终在视图中心显示弹出窗口?地图箱

How to always show the popup in the centre of the view? Mapbox

我想要一个始终出现在中心的弹出窗口 view.If 我将地图向右移动了一点,比如 Boxhill,然后单击按钮,弹出窗口将很难看到.我该如何解决这个问题?

https://jsfiddle.net/dendi_lee1996/j985mpvz/20/

HTML

<div id="map"></div>
<nav id="button"></nav>

CSS

body {
  margin: 0;
  padding: 0;
}

#map {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 100%;
}

#button {
  background: #fff;
  position: absolute;
  z-index: 1;
  top: 10px;
  right: 10px;
  border-radius: 3px;
  width: 120px;
  border: 1px solid rgba(0, 0, 0, 0.4);
  font-family: 'Arial', sans-serif;
}

#button tab {
  font-size: 13px;
  color: #404040;
  display: block;
  margin: 0;
  padding: 0;
  padding: 10px;
  text-decoration: none;
  border-bottom: 1px solid rgba(0, 0, 0, 0.25);
  text-align: center;
}

#button tab:last-child {
  border: none;
}

#button tab:hover {
  background-color: #f8f8f8;
  color: #404040;
}

#button tab.active {
  background-color: #3887be;
  color: #ffffff;
}

#button tab.active:hover {
  background: #3074a4;
}

提前致谢。

丹尼

您可以动态设置弹出窗口的位置(参见 https://docs.mapbox.com/mapbox-gl-js/api/markers/#popup#setlnglat)。像这样:

if (!popup.isOpen()) {
    this.className = '';
    popup.setLngLat(map.getCenter());
    popup.addTo(map)
} else {
    this.className = 'active';
    popup.remove();
}