简体   繁体   English

mapbox-gl-js 过滤器或数据驱动属性

[英]mapbox-gl-js filter or data-driven properties

This a question is for someone that knows how mapbox-gl-js works internally.这个问题是针对知道 mapbox-gl-js 如何在内部工作的人的。

Use case: we want to display 2 polygons with 2 different colors depending on the property region.用例:我们希望根据属性区域显示具有 2 个不同 colors 的 2 个多边形。

Currently, using mapbox-gl-js, you have 2 way of defining a style depending on data:目前,使用 mapbox-gl-js,您有两种根据数据定义样式的方法:

  1. Using filter:使用过滤器:
{
  "id": "region_sud",
  "filter": [
    "all",
    [
      "==",
      "sud",
      ["get", "region"]
    ]
  ],
  "paint": {
    "fill-color": "#F27E00",
  },
  "source": "region",
  "source-layer": "region",
  "type": "fill"
},{
  "id": "region_nord",
  "filter": [
    "all",
    [
      "==",
      "nord",
      ["get", "region"]
    ]
  ],
  "paint": {
    "fill-color": "#007E00",
  },
  "source": "region",
  "source-layer": "region",
  "type": "fill"
}
  1. Use data-driven style property使用数据驱动的样式属性
{
  "id": "region",
  "paint": {
    "fill-color": [
      "case",
      ["==", ["get", "region"],"sud"],
      "#F27E00",
      ["==", ["get", "region"],"nord"],
      "#007E00",
      "#000000",
    ],
  },
  "source": "region",
  "source-layer": "region",
  "type": "fill"
}

I would say the best option is the method 2 because we only have to create one style instead of 2.我想说最好的选择是方法 2,因为我们只需要创建一种样式而不是 2。

But what if there is 20 or 200 differents regions to draw.但是,如果要绘制 20 或 200 个不同的区域怎么办。 Which solution will be the best in terms of performance?哪种解决方案在性能方面是最好的?

If you define 20- 200 layers, map rendering becomes slow... So of course considering them as a single layer and using data driven styles is a better approach.如果定义 20-200 层,map 渲染会变慢……所以当然将它们视为单层并使用数据驱动的 styles 是更好的方法。

you can take a look at the documentation你可以看看文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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