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

标签 mapbox mapbox-gl-js mapbox-gl mapbox-gl-draw

我在看 a GeoJSON file并使用 draw.set(geoJSON) 将多边形(和其他东西)导入 mapbox-gl draw .如何通过要素属性中的属性为单个多边形着色。例子:

{
  "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 为绿色。我们怎么做?

最佳答案

您需要设置 userProperties a feature will be available for styling 的属性为真.并使用前缀 user .

并使用 case expression :

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/ ]

关于mapbox - 如何为使用 mapbox-gl draw 绘制的单个多边形着色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51303181/

相关文章:

geolocation - MapBox 通过 IP 地址进行地理定位

javascript - "GL_INVALID_OPERATION: Insufficient buffer size."在可变数量的渲染调用之后

mapbox - 无法让自定义标记在 Mapbox GL JS 中工作

ios - Mapbox iOS SDK 3.1 如何让Annotation Callout 有粘性

ios - 将委托(delegate)添加到 CalloutView

javascript - 单击标记 map 框

three.js - Three.js 中的 Raycast 只有一个投影矩阵

react-native - 沿线移动标记、React native、Mapbox 或 react native map

ios - 有没有办法防止在 MapboxGL for iOS 中滚动时标注消失?

iOS Mapbox 自定义标记不会出现