【问题标题】:Leaflet: Pass an extra argument to L.geoJson options传单:向 L.geoJson 选项传递一个额外的参数
【发布时间】:2013-02-08 16:50:58
【问题描述】:

我正在使用 Leaflet 框架处理等值线图。我想有几个单独的层几年,所以我写了这段代码(注意只有'style2002'和'style2010'的名字应该被传递,没有任何参数):

        population2002 = L.geoJson(regionData, {
        style: style2002
    });

    population2010 = L.geoJson(regionData, {
        style: style2010
    });

,根据属性(名称是前缀“Pop_”加上年份)为我的矢量多边形着色的“样式”函数是:

        function style2002(feature)
    {
        return {
            fillColor: getColor(feature.properties.Pop_2002),
            weight: 2,
            opacity: 1,
            color: 'white',
            dashArray: '',
            fillOpacity: 0.7
        };
    }

    function style2010(feature)
    {
        return {
            fillColor: getColor(feature.properties.Pop_2010),
            weight: 2,
            opacity: 1,
            color: 'white',
            dashArray: '',
            fillOpacity: 0.7
        };
    };

您可以猜到,我想为我需要的每一年使用一个“样式”函数而不是单独的函数。比如:

        function styleByYear(feature, year)
    {   
        var property = 'feature.properties.Pop_';
        property += year;
        return {
            fillColor: getColor(property),
            weight: 2,
            opacity: 1,
            color: 'white',
            dashArray: '',
            fillOpacity: 0.7
        };
    }

但是如何将第二个参数传递给样式函数呢?在 L.geoJson 构造函数中,我只写了函数的名称,没有任何参数,正如您从第一段代码中看到的那样! 我该怎么办? 还有一个问题:如何将第一个参数('feature')传递给层构造函数..?

【问题讨论】:

    标签: javascript oop web-applications gis leaflet


    【解决方案1】:

    如果你创建一个全局变量会怎样:

    var property = 'Pop_' + year
    

    并将您的函数编辑为以下内容(您应该使用括号而不是点表示法):

        function styleByYear(feature)
    {   
    
        return {
            fillColor: getColor(feature['properties'][property]),
            weight: 2,
            opacity: 1,
            color: 'white',
            dashArray: '',
            fillOpacity: 0.7
        };
    }
    

    根据像您这样的 choropleth 教程,我已经完成了与您所要求的类似的事情。我有多个按钮可以更改不同日期的地图样式。

    【讨论】:

      【解决方案2】:

      您可以尝试Leaflet tutorial on GeoJSON 中的内容。在“选项”部分中查找第二个代码部分。您只需先添加正常样式(即两年都相同的东西)。取自该站点的示例,添加您的特定代码:

      geojsonlayer = L.geoJson(regionData, {
        style: function(feature) {
          var finalStyleObject = normalStyling(feature); //return the normal style object
          switch (feature.properties) { //switch through the various years
              case 'Pop_2002': {finalStyleObject.fillColor: "#ff0000"};
              case 'Pop_2010': {finalStyleObject.fillColor: "#0000ff"};
          }
          return finalStyleObject;
        }
      });
      
      function normalStyling(feature){
        return {
              weight: 2,
              opacity: 1,
              color: 'white',
              dashArray: '',
              fillOpacity: 0.7
          };
      }
      

      【讨论】:

        猜你喜欢
        • 2016-05-17
        • 2012-03-13
        • 2018-01-15
        • 2012-06-01
        • 1970-01-01
        • 2012-05-28
        • 2016-02-17
        • 2017-06-01
        • 2012-03-23
        相关资源
        最近更新 更多