简体   繁体   中英

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

I'm trying to apply opacity to a geojson layer in leaflet.js . The documentation seems to show that opacity can be set in the style configuration.

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 does get applied to the resulting exteriorMaskLayer , and the polygon is displayed.

However, the opacity value appears to be ignored.

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

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?

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

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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