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

梦想的初衷 提交于 2019-12-25 06:58:24

问题


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",
   });

回答1:


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"]
});


来源:https://stackoverflow.com/questions/36923916/render-a-featurecollection-from-a-geojson-file-with-multiple-types-in-mapbox-gl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!