ryan
ryan

Reputation: 1

Render a FeatureCollection from a GeoJSON file with multiple types in Mapbox-GL-JS

We are currently loading layers into mapbox GL from geojson data. If our geojson has a feature collection that contains points and polygons, there does not seem to be a way to have mapbox gl show both because of how you need to set the type of layer.

Is there a way to have multiple types for a layer? It seems as if it can't handle multiple.

   map.addLayer({
    "id": "route",
    "type": "line", //THIS SEEMS TO BE THE LIMITATION
    "source": "route",
   });

Upvotes: 0

Views: 1767

Answers (1)

Lucas Wojciechowski
Lucas Wojciechowski

Reputation: 3802

You are correct, GL JS cannot handle multiple types per layer.

You can, however, display multiple geometry types from a single source by creating multiple layers:

map.addLayer({
    "id": "route-line",
    "type": "line",
    "source": "route",
    "filter": ["==", "$type", "LineString"]
});

map.addLayer({
    "id": "route-point",
    "type": "circle",
    "source": "route",
    "filter": ["==", "$type", "Point"]
});

map.addLayer({
    "id": "route-fill",
    "type": "fill",
    "source": "route",
    "filter": ["==", "$type", "Polygon"]
});

Upvotes: 5

Related Questions