6
votes

The examples for highlighting polygons in mapbox gl use a second layer and a filter function. Is it not possible to change the color of a single feature/polygon in mapbox gl js, drawn from geojson source?

See https://www.mapbox.com/mapbox-gl-js/example/hover-styles/

1

1 Answers

0
votes

It's possible to style a single feature using a data driven style that responds uniquely to an attribute of a single feature. For instance, if you have a point dataset with an id attribute and you want id 450 to be yellow instead of blue.

{
  "id": "mypoints",
  "type": "circle",
  "paint": {
    "fill-color": {
      "property": "id",
      "type": "category",
      "stops": [[450, "yellow"]],
      "default": "blue"
    }
  }
}

Caveat 1: the "default" feature is not publicly available yet.

Caveat 2: This only works for style properties that support data-driven functions.