setOpacity for multiple markers at the time

雨燕双飞 提交于 2021-01-28 07:47:16

问题


I'm using leaflet for my project and I want to use filter marker in it. To do it, I will setOpacity to 0 for all markers and re setOpacity to 1 for my targets. I know leaflet allow to setOpacity for each market but can I set all markers at the same time? Thanks for your help!


回答1:


There are many ways to achieve that

In leaftlet

Create a layer group and add each marker to this group :

var myGroup = L.layerGroup([mark1, mark2, ...]);

You can add the entire group to the map.

Then, when you want to set marker opacity to 0 do :

myGroup.eachLayer(function(layer) {
    layer.setOpacity(0);
});

A little jsfiddle example here :

https://jsfiddle.net/csblo/64phqLb7/4/

In pure javascript

Store all your markers in an array. First create an array :

var allMarkers = [];

And when you create a new marker push it in this array :

var marker = L.marker(...);
allMarkers.push(marker);

Then, when you have to set opacity to 0 :

allMarkers.forEach(function(marker) {
    marker.setOpacity(0);
});


来源:https://stackoverflow.com/questions/43339457/setopacity-for-multiple-markers-at-the-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!