Reputation: 5525
Given a Mapbox tile source and a layer-name, how do I know this layer's type is (point, line, polygon)?
I want know the feature type ahead of time before I add the layer to a map (with map.addLayer
)
if (layer-type 'point'){
// for point only
map.addLayer({
type: circle
}
} else {
// for both lineString and polygon
map.addLayer({
type: line
}
}
Upvotes: 1
Views: 266
Reputation: 5525
I kind find a way to solve this problem.
Reference https://docs.mapbox.com/mapbox-gl-js/example/multiple-geometries/
Add multiple geometries from one GeoJSON source,
The key is:
you add all 3 layer for point, line, polygon, with 3 different layer-id each layer, you use filter for that type only
map.addLayer(
"type": fill
"filter": ["==", "$type", "Polygon"]
)
map.addLayer(
"type": line
"filter": ["==", "$type", "LingString"]
)
map.addLayer(
"type": Circle, // or symbol
"filter": ["==", "$type", "Point"]
)
I have a complete code at: https://github.com/hoogw/arcgis_viewer/blob/master/public/javascripts/pbf/mapbox_common_share.js
Upvotes: 1