简体   繁体   English

如何在地图框中单击标记时更改标记颜色和图标?

[英]How to change marker color and icon on clicking a marker in mapbox?

I am new to Mapbox. 我是Mapbox的新手。 I have created a custom style map in mapbox studio and then add the markers using geoJson. 我在mapbox studio中创建了一个自定义样式贴图,然后使用geoJson添加标记。 Here is the sample code: 以下是示例代码:

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

});

Now I want to change the marker color on clicking any of the marker to display the selected marker. 现在我想在点击任何标记时更改标记颜色以显示所选标记。 Thanks in advance. 提前致谢。

Thanks for the question! 谢谢你的提问!

You will find this example helpful in implementing the functionality you've requested: https://www.mapbox.com/mapbox-gl-js/example/hover-styles/ 您会发现此示例有助于实现您请求的功能: https//www.mapbox.com/mapbox-gl-js/example/hover-styles/

The basic workflow is as follows: 基本工作流程如下:

  • create two layers of markers: one that displays markers in the unhighlighted style and one that displays markers in the highlighted style 创建两层标记:一层显示未突出显示样式的标记,另一层以突出显示的样式显示标记
  • hide all markers from the highlighted styled layer using a filter 使用过滤器隐藏突出显示的样式图层中的所有标记
  • listen for the click event 听取click事件
  • find the marker under the cursor using queryRenderedFeatures 使用queryRenderedFeatures在光标下找到标记
  • display the marker on the highlighted layer using a filter 使用过滤器在突出显示的图层上显示标记

We are working on ways to make this process more straightforward! 我们正在努力使这个过程更简单!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM