【问题标题】:How to always show the popup in the centre of the view? Mapbox如何始终在视图中心显示弹出窗口?地图盒
【发布时间】:2020-10-27 12:44:43
【问题描述】:

我想要一个始终出现在视图中心的弹出窗口。如果我将地图向右移动一点,比如 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;
}

提前致谢。

丹尼

【问题讨论】:

    标签: html mapbox mapbox-gl-js


    【解决方案1】:

    您可以动态设置弹出窗口的位置(请参阅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();
    }
    

    【讨论】:

      猜你喜欢
      • 2013-01-06
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      • 2012-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-13
      相关资源
      最近更新 更多