【问题标题】:Changing style in OpenLayers clustered features改变 OpenLayers 集群特征的风格
【发布时间】:2019-03-22 19:30:43
【问题描述】:

我正在使用带有 Javascript 的 OpenLayers,并在地图上显示集群特征。我想根据其属性值之一更改群集内功能的图标。

var style1 = new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({
        anchor: [0.5, 66],anchorXUnits: 'fraction',anchorYUnits: 'pixels',
        opacity: 0.85,src: 'https://img.icons8.com/flat_round/64/000000/home.png',scale: 0.3
      }));
      var style2 = new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({
        anchor: [0.5, 66],anchorXUnits: 'fraction',anchorYUnits: 'pixels',
        opacity: 0.85,src: 'https://img.icons8.com/color/48/000000/summer.png',scale: 0.3
      }));
      function myStyleFunction(feature) {
        let props = feature.getProperties();
        if (props.id>50) {
          console.log(props.id);
          return new ol.style.Style({image: style1,stroke: new ol.style.Stroke({ color:"#fff", width:1 }) });
        } else {
          console.log(props.id);
          return new ol.style.Style({image: style2,stroke: new ol.style.Stroke({ color:"#fff", width:1 }) });
        }
      };

在上面的代码中,我想访问集群中某个功能的属性“id”,并根据“id”值设置其图标。但是,我无法获取功能属性。

这是codepen。我会感谢任何人的帮助。

【问题讨论】:

    标签: json mapping openlayers openlayers-3


    【解决方案1】:

    如果只检查每个集群中的第一个特征:

      function myStyleFunction(feature) {
        let props = feature.get('features')[0].getProperties();
        if (props.id>50) {
          console.log(props.id);
          return new ol.style.Style({image: style1,stroke: new ol.style.Stroke({ color:"#fff", width:1 }) });
        } else {
          console.log(props.id);
          return new ol.style.Style({image: style2,stroke: new ol.style.Stroke({ color:"#fff", width:1 }) });
        }
      };
    

    如果你想在集群中的任何特征中找到值

      function myStyleFunction(feature) {
        let maxId = 0;
        feature.get('features').forEach(function(feature){
          maxId = Math.max(maxId, feature.getProperties().id);
        });
        if (maxId>50) {
          console.log(maxId);
          return new ol.style.Style({image: style1,stroke: new ol.style.Stroke({ color:"#fff", width:1 }) });
        } else {
          console.log(maxId);
          return new ol.style.Style({image: style2,stroke: new ol.style.Stroke({ color:"#fff", width:1 }) });
        }
      };
    

    对于 ol-ext 集群

      function myStyleFunction(feature) {
        let id = 0;
        let features = feature.get('features');
        if (features) {
          id = features[0].get('id');
        }
        if (id > 50) {
          return new ol.style.Style({image: style1,stroke: new ol.style.Stroke({ color:"#fff", width:1 }) });
        } else {
          return new ol.style.Style({image: style2,stroke: new ol.style.Stroke({ color:"#fff", width:1 }) });
        }
      };
    

    【讨论】:

    • 我尝试了您的两个建议,但它们都没有改变集群中任何功能的样式。让我重复一下需求:根据每个特征的“id”属性值,特征本身必须采用各自的样式。例如,如果集群有两个 50 的特征,则必须有两个 style1 的特征和两个 style2 的特征。
    • 如果你想显示单个特征,它不是一个集群。或者您是否想为大于和小于 50 的特征显示单独的集群计数?然后你需要两个集群层。
    • 我问题中的 codepen 表明,通过单击集群,它会展开并显示所有单独的功能。我需要用相应的图标显示它们。
    • ol-ext 集群层的工作方式与普通 OpenLayers 集群不同,样式函数为每个原始特征调用一次,该特征作为特征数组的唯一条目。它也是在没有特征数组的情况下调用的,这导致我的第一个方法出错。
    • 就是这样,完美运行!我没有意识到 ol-ext 在集群层方面有所不同。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-03
    • 2022-12-16
    • 1970-01-01
    • 2017-04-01
    • 2017-06-09
    • 2016-11-24
    相关资源
    最近更新 更多