mapbox: выражение цвета заливки в зависимости от значения (не значения цвета) из geojson

#maps #expression #mapbox #geojson

#Карты #выражение #mapbox #geojson

Вопрос:

как я мог бы установить цвет фигуры в зависимости от значения одного из ее свойств в GeoJSON?

допустим, у меня есть эта коллекция функций :

 {
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "district_type": "foo"
      },
      "geometry": {
        "coordinates": [...],
        "type": "Polygon"
      }
    },
    {
      "type": "Feature",
      "properties": {
        "district_type": "bar"
      },
      "geometry": {
        "coordinates": [...],
        "type": "Polygon"
      }
    }
  ]
}
 

И этот массив для сопоставления цветов :

     var colors = {
      foo:'#111111',
      bar:'#222222',
    }
 

Теперь я хочу добавить слой, в котором фигуры имеют цвет из моей переменной colors, соответствующий свойству distric_type объектов.

 map.addLayer({
  'id': 'districts-fill',
  'type': 'fill',
  'source': 'geo-districts',
  'paint': {
    'fill-color': ..., //which expression here ?
    'fill-opacity': 0.4
  }
});
 

Спасибо!

Ответ №1:

Я это выяснил… Это так приятно!

 map.addLayer({
  'id': 'districts-fill',
  'type': 'fill',
  'source': 'geo-districts',
  'paint': {
    'fill-color': [
      'match', ['get', 'district_type'],
      'foo', '#111111',
      'bar', '#222222',
      'white'// otherwise
   ],
   'fill-opacity': 0.4
  },