Always show popups mapbox
Asked Answered
S

1

7

I've been following this tutorial https://docs.mapbox.com/help/tutorials/custom-markers-gl-js/

My markers is showing fine and I can click on the markers to show a popup, but I would like the popups to always be shown.

I've successfully modified the CSS to not show the arrow and "x / close" button.

var geojson = {
    "type": "FeatureCollection",
    "features": [{
         "type": "Feature",
         "geometry": {
             "type": "Point",
             "coordinates": [-77.032, 38.913]
         },
         "properties": {
             "title": "Mapbox",
             "description": "Washington, D.C."
         }
     },
     {
         "type": "Feature",
         "geometry": {
         "type": "Point",
         "coordinates": [-122.414, 37.776]
        },
         "properties": {
             "title": "Mapbox",
             "description": "San Francisco, California"
         }
     }]
};

var map = new mapboxgl.Map({
    container: 'map',
    style: 'mapbox://styles/mapbox/light-v10',
    center: [-96, 37.8],
    zoom: 3
});

// 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 it to the map
    new mapboxgl.Marker(el)
        .setLngLat(marker.geometry.coordinates)
        .setPopup(new mapboxgl.Popup({offset: 25}) // add popups
            .setHTML('<h3>' + marker.properties.title + '</h3><p>' + marker.properties.description + '</p>'))
        .addTo(map);
});

Any ideas?

Secundas answered 17/5, 2019 at 13:33 Comment(0)
P
14

I've successfully modified the CSS to not show the arrow and "x / close" button

To hide the "x" button, you could also use the closeButton option (see API reference).

but I would like the popups to always be shown.

Use togglePopup() which "opens or closes the bound popup, depending on the current state":

new mapboxgl.Marker(el)
    .setLngLat(marker.geometry.coordinates)
    .setPopup(new mapboxgl.Popup({closeOnClick: false, closeButton: false}).setText("some text"))
    .addTo(map)
    .togglePopup();
Pekoe answered 17/5, 2019 at 17:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.