fefe
fefe

Reputation: 9055

Mapbox GL JS markers show/hide

Using Mapbox GL JS and following the provided example which shows how to attach markers to the map

// add markers to map
geojson.features.forEach(function(marker) {

  // create a HTML element for each feature
  var el = document.createElement('div');
  el.className = 'marker';

  // make a marker for each feature and add to the map
  new mapboxgl.Marker(el)
    .setLngLat(marker.geometry.coordinates)
    .addTo(map);
});

how can I hide/show on click event different markers

Upvotes: 4

Views: 6726

Answers (1)

Anatolii Suhanov
Anatolii Suhanov

Reputation: 2702

Something like this?

function hide() {
    let markers = document.getElementsByClassName("marker");
    for (let i = 0; i < markers.length; i++) {
        markers[i].style.visibility = "hidden";
    }
}

function show() {
    let markers = document.getElementsByClassName("marker");
    for (let i = 0; i < markers.length; i++) {
        markers[i].style.visibility = "visible";
    }
}

Upvotes: 7

Related Questions