【发布时间】:2017-08-23 13:26:17
【问题描述】:
我想在悬停(而不是单击)时显示有关标记的弹出窗口。我已经查看了 Mapbox 上的所有示例 (https://www.mapbox.com/mapbox-gl-js/example/popup-on-hover/) -
但是示例中的问题是它从脚本中的图层中提取位置和描述...我希望它显示来自我的数据集图层 ['mydata'] 的数据
下面的代码适用于点击标记 - 我只是希望它在悬停时工作。
<script>
mapboxgl.accessToken = 'example-token'; // replace this with your access token
var map = new mapboxgl.Map({
container: 'map',
style: 'example-style' // replace this with your style URL
});
// code from the next step will go here
map.on('click', function(e) {
var features = map.queryRenderedFeatures(e.point, {
layers: ['pillbox-data'] // replace this with the name of the layer
});
if (!features.length) {
return;
}
var feature = features[0];
var popup = new mapboxgl.Popup({
offset: [0, -15]
})
.setLngLat(feature.geometry.coordinates)
.setHTML('<h3>' + feature.properties.title + '</h3><p>' + feature.properties.description + '</p>')
.setLngLat(feature.geometry.coordinates)
.addTo(map);
});
map.addControl(new MapboxGeocoder({
accessToken: mapboxgl.accessToken
}));
</script>
【问题讨论】:
-
找到这个演示如何做到这一点jsfiddle.net/rasagy/sy0j1nqj
标签: popup hover mapbox geojson