Dan
Dan

Reputation: 103

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

I would like to have a popup that always emerges in the centre of the view.If I moved the map to the right a bit, say Boxhill, then clicked the button, the popup would be hard to see. How am I supposed to solve this?

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;
}

Thanks in advance.

Danny

Upvotes: 1

Views: 1708

Answers (1)

Anatolii Suhanov
Anatolii Suhanov

Reputation: 2702

You can set the location of the popup dynamically (see https://docs.mapbox.com/mapbox-gl-js/api/markers/#popup#setlnglat). Something like this:

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

Upvotes: 1

Related Questions