mapbox-gl-js - 如何在 mapbox-gl-js 的标记上添加 "click"监听器

标签 mapbox-gl-js

我使用以下示例在 map 上添加了一些标记:

https://www.mapbox.com/mapbox-gl-js/example/geojson-markers/

现在我想在用户点击标记图像时显示弹出窗口,但我找不到优雅的解决方案。有什么帮助吗?

最佳答案

以下片段来自 Mapbox-gl-js 示例:Display a popup on click

 map.on('click', function (e) {
    var features = map.queryRenderedFeatures(e.point, { layers: ['places'] });

    if (!features.length) {
        return;
    }

    var feature = features[0];
    //Use Feature and put your code
    // Populate the popup and set its coordinates
    // based on the feature found.
    var popup = new mapboxgl.Popup()
        .setLngLat(feature.geometry.coordinates)
        .setHTML(feature.properties.description)
        .addTo(map);
});

   // Use the same approach as above to indicate that the symbols are clickable
   // by changing the cursor style to 'pointer'.

map.on('mousemove', function (e) {
    var features = map.queryRenderedFeatures(e.point, { layers: ['places'] });
    map.getCanvas().style.cursor = (features.length) ? 'pointer' : '';
});

关于mapbox-gl-js - 如何在 mapbox-gl-js 的标记上添加 "click"监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31448397/

相关文章:

javascript - 如何在 Rails Turbolink 中正确使用 jQuery $().load(url)?

javascript - 从本地文件在 Mapbox 中加载带有杂食动物的 csv

javascript - 检测软件与硬件 webGL 渲染模式

javascript - 如何设置传单中的图像偏移?

mapbox-gl-js - tippecanoe 在中级缩放时不断丢弃多边形

mapbox-gl-js - 使用 Mapbox GL JS 绘制半径为英里/米的圆

javascript - 如何使用 Mapbox GL JS 和对象制作等值线图?

javascript - 在现有示例的 Mapbox gl 上进行过滤

mapbox - 检查 Mapbox 视口(viewport)中是否存在 GeoJSON 源

mapbox - 如何在 Mapbox GL JS 中制作 3D 模型运动的动画