有人知道如何在mapboxgl中单击时展开标记弹出窗口/信息框吗?
目前,我所拥有的是以下内容。当用户将鼠标悬停在标记上时,会出现一个弹出窗口,显示地点的名称。
var infobox = new mapboxgl.Popup({
closeButton: false,
closeOnClick: false
})
map.on('mousemove', function(e) {
var features = map.queryRenderedFeatures(e.point, {
layers: [‘layer’]
});
map.getCanvas().style.cursor = (features.length) ? 'pointer' : '';
if (!features.length) {
infobox.remove();
return;
}
var feature = features[0];
infobox.setLngLat(feature.geometry.coordinates)
.setHTML(‘Venue name’)
.addTo(map);
});
除此之外,我需要实现的是:当用户单击标记时,弹出窗口会展开,并显示地点的名称和描述。弹出窗口保持打开状态,直到用户单击地图上的其他位置。有人知道如何在vanilla JS中做到这一点吗?
发布于 2019-03-22 21:29:24
你是说像这样吗?
// create DOM element for the marker
var el = document.createElement('div');
el.id = 'marker';
// create the popup
var popup = new mapboxgl.Popup({ offset: 25 })
.setText('Venue name');
new mapboxgl.Marker(el)
.setLngLat(monument)
.setPopup(popup) // sets a popup on this marker
.addTo(map);
https://stackoverflow.com/questions/55297936
复制