【问题标题】:MapBox Randomizing Building ColorMapBox 随机化建筑颜色
【发布时间】:2020-09-09 02:23:09
【问题描述】:

是否可以循环遍历地图上的所有建筑物并为它们分配不同的颜色或驱动主题分组的不同属性值?

我有代码可以遍历建筑物:

features = map.queryRenderedFeatures({ layers: ["building"], filter: ['==', 'extrude', 'true']});
features.forEach(function(feature){
  // how to change feature colour or property in here?
}

【问题讨论】:

    标签: mapbox-gl-js


    【解决方案1】:

    有几种方法可以实现您想要的。

    功能状态

    如果您的建筑物图层具有要素 ID,您可以使用 setFeatureState 为每个建筑物设置状态。

    features = map.querySourceFeatures('building', { sourceLayer: 'buildings' }));
    features.forEach(function(feature){
      if (!map.getFeatureState({ id: feature.id, source: 'building', sourceLayer: 'buildings' }).color) {
        map.setFeatureState({ id: feature.id, source: 'building', sourceLayer: 'buildings' }, { color: makeRandomColor() }
      }
    });
    

    然后您可以在表达式中使用['feature-state', 'color']

    准随机

    如果没有特征 ID,您可能会以一种看似随机的方式使用其他属性。例如,如果有其他类型的 ID,您可以使用 mod 函数将其映射到一种颜色,这种方式可能看起来有点随机。

    【讨论】:

      猜你喜欢
      • 2018-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-08
      • 2021-05-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多