javascript 如何在传单中设置 geojson 对象或图层的不透明度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16182595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 03:36:45  来源:igfitidea点击:

How can I set the opacity of a geojson object or layer in leaflet?

javascriptleaflet

提问by monkut

I'm trying to apply opacityto a geojson layer in leaflet.js. The documentationseems to show that opacitycan be set in the style configuration.

我想申请opacity到一个GeoJSON的层leaflet.js。该文件似乎表明,opacity可以在风格配置进行设置。

var exteriorStyle = {
    "color": "#ffffff",
    "weight": 0,
    "opacity": 0.99
};

var exteriorMaskLayer = L.geoJson(exteriorMaskGeojsonPoly, {style: exteriorStyle}).addTo(map);

I'd like the object to mask/hide the background map. Here, using exteriorStyle, the color doesget applied to the resulting exteriorMaskLayer, and the polygon is displayed.

我希望对象屏蔽/隐藏背景地图。在这里,使用exteriorStyle,颜色确实应用于结果exteriorMaskLayer,并显示多边形。

However, the opacityvalue appears to be ignored.

但是,该opacity值似乎被忽略了。

I've also tried using the setOpacity()method of the exteriorMaskLayerwith no effect.

我也试过用没有效果的setOpacity()方法exteriorMaskLayer

var exteriorMaskLayer = L.geoJson(exteriorMaskGeojsonPoly, {style: exteriorStyle}).addTo(map);
exteriorMaskLayer.setOpacity(1.0);

How can I set the opacity of a geojson object or layer in leaflet?

如何在传单中设置 geojson 对象或图层的不透明度?

using Leaflet-Leaflet-v0.5.1-0-gc1d410f.zip

使用 Leaflet-Leaflet-v0.5.1-0-gc1d410f.zip

回答by monkut

Doh, I found it browsing some of the other leaflet documentation. The style attribute I needed was fillOpacity.

Doh,我发现它浏览了其他一些传单文档。我需要的样式属性是fillOpacity.

I guess opacityis only applied to the border.
weight, here, turns off the border, so I didn't notice any change.

我猜opacity只适用于边界。
weight,在这里,关闭了边框,所以我没有注意到任何变化。

So this works, applying opacity to the interior of a polygon:

所以这是有效的,将不透明度应用于多边形的内部:

var exteriorStyle = {
    "color": "#ffffff",
    "weight": 0,
    "fillOpacity": .75
};

var exteriorMaskLayer = L.geoJson(exteriorMaskGeojsonPoly, {style: exteriorStyle}).addTo(map);

I couldn't find any docs on the available style attributes.

我找不到有关可用样式属性的任何文档。