如何在传单中设置 geoJSON FeatureCollection 的样式?

How do I style a geoJSON FeatureCollection in leaflet?

我正在尝试使用 L.geoJSON 设置 geoJSON FeatureCollection 的样式,但我未能成功偏离默认样式。

我已经尝试了 L.geoJSON 的文档(见下文),并将样式属性直接放入 geoJSON 功能中。两个选项都对图层的显示没有影响

//coordinates on map
var map = L.map('map').setView([29.76, -95.37], 10);
L.esri.basemapLayer('Gray').addTo(map);
var fromProjection = "+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +a=6378137 +b=6378137 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs";
var toProjection = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs ";
var myStyle = {
    "color": "#ff7800",
    "weight": 5,
    "opacity": 0.65
};
var myLayer = L.geoJSON(watersheds,{
                coordsToLatLng: function (p) {
                    p = proj4(fromProjection,toProjection,p);  // reproject each point
                    p = [p[1],p[0]]    // swap the values
                    return p;          // return the lat/lng pair
                }},{
                style: myStyle
}).addTo(map);

预期结果:我的层将是橙色的并且略微透明

实际效果:我的图层是默认的蓝色

(类似于:http://bl.ocks.org/andrew-reid/e472886c83819e8459178a4a56548449

试试这个:

//coordinates on map
    var map = L.map('map').setView([29.76, -95.37], 10);
    L.esri.basemapLayer('Gray').addTo(map);
    var fromProjection = "+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +a=6378137 +b=6378137 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs";
    var toProjection = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs ";
    var myStyle = {
        "color": "#ff7800",
        "weight": 5,
        "opacity": 0.65
    };
    myLayer = L.geoJson(watersheds, {
       onEachFeature: function (feature, layer) {
       layer.setStyle(feature.myStyle);

       }
    }).addTo(map);

我找到问题了

这方面没有很好的文档,但 L.geoJSON 的第二个参数是 'options'。我无意中在 coordsToLatLong 之后结束了争论。下面粘贴了此案例的正确代码。

var map = L.map('map').setView([29.76, -95.37], 10);
    L.esri.basemapLayer('Gray').addTo(map);
    var fromProjection = "+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +a=6378137 +b=6378137 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs";
    var toProjection = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs ";
    var myStyle = {
        "color": "#ff7800",
        "weight": 5,
        "opacity": 0.65
    };
    var myLayer = L.geoJSON(watersheds,{
                    style: myStyle,
                    coordsToLatLng: function (p) {
                        p = proj4(fromProjection,toProjection,p);  // reproject each point
                        p = [p[1],p[0]]    // swap the values
                        return p;          // return the lat/lng pair
                    }}).addTo(map);

您可以使用 console.log(myLayer.options)

检查您的答案