gpt4 book ai didi

javascript - 如何在单击 map 框中的标记时更改标记颜色和图标?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:31:52 25 4
gpt4 key购买 nike

我是 Mapbox 的新手。我在 mapbox studio 中创建了一个自定义样式 map ,然后使用 geoJson 添加标记。这是示例代码:

mapboxgl.accessToken = 'pk.eyJ1Ijoic2Fua3ljc2Uhhcc.mb22KHuonjywQ-eaWQ';
var map = new mapboxgl.Map({
container: 'map_geo',
style: 'mapbox://styles/abcd/cipjtsdhyh04ebam5tndf4jaj',
zoom: 3.7,
center: [81.30, 22.76]
});
var geoJson = {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {
"title": "Nagpur",
"description": "Nagpur",
"marker-symbol": "marker",
},
"geometry": {
"coordinates": [79.0882, 21.1845],
"type": "Point"
},
"id": "223e9579f03849c87abec10dfed64c37"
}, {
"type": "Feature",
"properties": {
"title": "Lucknow",
"description": "Lucknow",
"marker-symbol": "marker",
},
"geometry": {
"coordinates": [80.9462, 26.8467],
"type": "Point"
},
"id": "2cc757705489152c8bccb33635708427"
}]
};

map.on('load', function () {
map.addSource("markers", {
"type": "geojson",
"data": geoJson

});

map.addLayer({
"id": "markers",
"type": "symbol",
"source": "markers",
"layout": {
"icon-image": "{marker-symbol}-15",
"text-field": "{title}",
"text-font": ["Open Sans Semibold", "Arial Unicode MS Bold"],
"text-offset": [0, 0.6],
"text-anchor": "top",
},
"paint": {
"text-size": 16,
"text-color": "#fff",
}
});
});
map.scrollZoom.disable();
map.on('click', function (e) {
var features = map.queryRenderedFeatures(e.point, { layers: ['markers'] });

if (!features.length) {
return;
}

// here I want to change the marker color to highlight the selected marker

});

现在我想通过单击任何标记来更改标记颜色以显示所选标记。提前致谢。

最佳答案

感谢提问!

您会发现此示例有助于实现您请求的功能:https://www.mapbox.com/mapbox-gl-js/example/hover-styles/

基本工作流程如下:

  • 创建两层标记:一层以未突出显示的样式显示标记,另一层以突出显示的样式显示标记
  • 使用过滤器隐藏高亮样式层中的所有标记
  • 监听点击事件
  • 使用 queryRenderedFeatures 找到光标下的标记
  • 使用滤镜在高亮图层上显示标记

我们正在努力使这个过程更简单!

关于javascript - 如何在单击 map 框中的标记时更改标记颜色和图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38068820/

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