如何为使用mapbox-gl draw绘制的单个多边形着色?

7

我正在阅读GeoJSON文件,并使用draw.set(geoJSON)将多边形(和其他内容)导入到Mapbox-gl绘图中。如何通过要素属性中的属性来为单个多边形着色。例如:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      //"id": "the most unique id in the world",
      "properties": {
        "class_id": 1
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              79.30961608886719,
              61.57192958204744
            ],
            [
              79.34309005737303,
              61.57192958204744
            ],
            [
              79.34309005737303,
              61.57871162332267
            ],
            [
              79.30961608886719,
              61.57871162332267
            ],
            [
              79.30961608886719,
              61.57192958204744
            ]
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "class_id": 2
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              79.35201644897461,
              61.58271478278019
            ],
            [
              79.35115814208984,
              61.573972521656835
            ],
            [
              79.38188552856444,
              61.57192958204744
            ],
            [
              79.35201644897461,
              61.58271478278019
            ]
          ]
        ]
      }
    },
    }

这个想法是将 class_id = 1 的特征标记为红色,将 class_id = 2 的标记为蓝色,将 class_id = 3 的标记为绿色。我们该如何实现呢?

1个回答

13

你需要将userProperties设置为true,以便可以对要素的属性进行样式设置。并使用前缀user

同时使用case表达式:

var Draw = new MapboxDraw({
  userProperties: true,
  styles: [{
      'id': 'gl-draw-polygon-fill-inactive',
      'type': 'fill',
      'filter': ['all', ['==', 'active', 'false'],
        ['==', '$type', 'Polygon'],
        ['!=', 'mode', 'static']
      ],
      'paint': {
        'fill-color': [
          "case", 
          ['==', ['get', "user_class_id"], 1], "#00ff00", 
          ['==', ['get', "user_class_id"], 2], "#0000ff",
          '#ff0000'
        ],
        'fill-outline-color': '#3bb2d0',
        'fill-opacity': 0.5
      }
    }...

[ http://jsfiddle.net/5Lotf4ka/ ]


谢谢!这就做到了。 - Shravan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接