gpt4 book ai didi

Mapbox 鼠标悬停圆圈以使用特征状态更改颜色

转载 作者:行者123 更新时间:2023-12-03 22:14:40 25 4
gpt4 key购买 nike

我在 Mapbox map 上的鼠标悬停期间使标记更改颜色时遇到问题。
0.47 版本的 mapbox-gl.js 允许你使用 feature-state更改动态样式的功能。

这适用于 Mapbox 示例:https://www.mapbox.com/mapbox-gl-js/example/hover-styles/

然而,在我的例子中,当特征标记的 feature-state 似乎永远不会触发样式。变化。

我设法使用 map.setFilter 使鼠标悬停工作函数,但是当我添加 1000 个标记时,这变得非常低效和缓慢。

任何提示将不胜感激,非常感谢。

请看我的示例代码...

<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>Create a hover effect</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.47.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.47.0/mapbox-gl.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
/* #map { position:absolute; top:0; bottom:0; width:100%; } */
#map { width:100%; height: 500px; }
</style>
</head>
<body>

<div id='map'></div>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoiY29kYm9kIiwiYSI6IjhjbFE1aUUifQ.Gimi98Oh3Uex9WQZlb5Wkw';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v9',
center: [-0.1507524, 51.5140731],
zoom: 9
});
var hoveredStateId = null;

map.on('load', function () {
map.addSource("sites", {
"type": "geojson",
"data": {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-0.1407524,
51.5240731
]
},
"properties": {
"id": 2,
"name": "Oxford Street"
}
}
]
}
});

// The feature-state dependent fill-opacity expression will render the hover effect
// when a feature's hover state is set to true.
map.addLayer({
'id': 'sites-bold',
'type': 'circle',
'source': 'sites',
'layout': {},
'paint': {
'circle-radius': 10,
// 'circle-color': '#ff442b',
'circle-color': ['case',
['boolean', ['feature-state', 'hover'], false],
'red',
'blue'
],
'circle-opacity': 0.6,
'circle-stroke-color': 'cyan',
'circle-stroke-width': 1
}
});


// When the user moves their mouse over the state-fill layer, we'll update the
// feature state for the feature under the mouse.
map.on("mousemove", "sites-bold", function(e) {
// console.log('in', e.features)
if (e.features.length > 0) {
// if (this.hoveredStateId) {
// map.setFeatureState({source: 'sites', id: this.hoveredStateId}, { hover: false});
// }
this.hoveredStateId = e.features[0].properties.id;
console.log('in-f-id', this.hoveredStateId)
map.setFeatureState({source: 'sites', id: this.hoveredStateId}, { hover: true});
console.log('in-f', e.features[0])
}
console.log('in', map.getFeatureState({source: 'sites', id: this.hoveredStateId}))
});

// When the mouse leaves the state-fill layer, update the feature state of the
// previously hovered feature.
map.on("mouseleave", "sites-bold", function() {
console.log('out-id', this.hoveredStateId)
// if (this.hoveredStateId) {
map.setFeatureState({source: 'sites', id: this.hoveredStateId}, { hover: false});
// }
console.log('out', map.getFeatureState({source: 'sites', id: this.hoveredStateId}))
hoveredStateId = null;
});
});
</script>

</body>
</html>

最佳答案

使用 setFeatureState ,您需要为数据源中的每个要素设置唯一 ID。所以实际上您需要做的就是添加一个 ID,如下所示:

map.addSource("sites", {
"type": "geojson",
"data": {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"id": 2,
"geometry": {
"type": "Point",
"coordinates": [
-0.1407524,
51.5240731
]
},
"properties": {
"id": 2,
"name": "Oxford Street"
}
}
]
}
});

请注意 id特征上的属性。将其嵌套在内部 properties不够;它必须位于该功能的顶层。该方法的文档应该在本周发布下一个版本时更新。

关于Mapbox 鼠标悬停圆圈以使用特征状态更改颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51755784/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com