javascript - 在 Mapbox 上未触发 mouseenter/mouseleave/click
问题描述
我的 HTML 上有以下 javascript 来使用 Mapbox 渲染地图
mapboxgl.accessToken = "<token>";
var map = new mapboxgl.Map({
"container": "mapFront",
"style": "mapbox://styles/mapbox/satellite-streets-v11",
"center": [-47.909, -22.087],
"zoom": 7
});
map.addControl(new mapboxgl.ScaleControl({position: "bottom-right"}));
map.addControl(new mapboxgl.NavigationControl());
map.on("load", function() {
map.addSource("focos", {
type: "geojson",
data: "https://localhost:5001/Home/Focos48H"
});
map.addLayer({
"id": "focos",
"type": "circle",
"source": "focos",
"paint": {
"circle-radius": 8,
"circle-stroke-width": 2,
"circle-color": "red",
"circle-stroke-color": "white"
}
});
map.on('click', 'focos', function(e) {
var c = e.features[0].geometry.coordinates.slice();
var desc = e.features[0].properties.resumo;
while (Math.abs(e.lngLat.lng - c[0]) > 180) {
c[0] += e.lngLat.lng > c[0] ? 360 : -360;
}
new mapboxgl.Popup().setLngLat(c).setHTML(desc).addTo(map);
});
map.on('mouseenter', 'focos', function() { map.getCanvas().style.cursor = 'pointer'; });
map.on('mouseleave', 'focos', function() { map.getCanvas().style.cursor = ''; });
});
GeoJSON 源码是这样的
{
"type":"FeatureCollection",
"features":[
{
"type":"Feature",
"id":"15150",
"geometry":{
"type":"Point",
"coordinates":[-51.99000000, -21.57000000 ]
},
"properties":{
"resumo":"Foco detectado em 2021-05-26T14:53:11"
}
},
{
"type":"Feature",
"id":"15151",
"geometry":{
"type":"Point",
"coordinates":[ -52.01000000, -21.57000000 ]
},
"properties":{
"resumo":"Foco detectado em 2021-05-26T14:53:11"
}
}
]
}
地图加载正常。GeoJSON 源加载正常。图层显示正确,但我有两个问题:
- 鼠标在进入/离开特征时不会改变形状,并且;
- 单击功能不会显示弹出窗口。
我在这里想念什么?
谢谢
解决方案
推荐阅读
- mysql - 如何在 Python3 中修复“'int' 类型的对象没有 len()”
- excel - 如何执行索引匹配类型过程以复制值并填充列(附加查询)
- scala - 对模拟对象的验证不尊重测试用例
- php - 修复 PHP 中的图像旋转
- sql-server - 如何在 WHERE 语句中使用 Case 或 Coalesce 检查列是否为 NULL
- linux - 命令显示正确,但执行方式不同
- node.js - 如何将多个 ReadableStreams 通过管道传输到单个 WriteStream?
- visual-studio - Visual Studio 解决方案资源管理器 - 观察到资产文件的不同图标
- php - 如何格式化关联数组
- java - 如何在 Java 中使用 ta-lib 修复 RSI 计算输出中的零点?