在 mapbox gl 中使用 getSource 或 getLayer 获取几何(坐标)

Get geometry(coord) with getSource or getLayer in mapbox gl

我有 5 个 geojson 层 - 每个层都有一行。我想获取带有 map.getSource 或 map.getLayer 的线的坐标,但我没有看到任何几何属性。 我可以用这个得到坐标:

var features = map.queryRenderedFeatures({layers:[layerName]});
console.log(features[0].geometry); 

我的 geojson 是:

var layer = map.addLayer({
        "id": layerName,
        "type": "line",
        "source": {
            "type": "geojson",
            "data": {
                "type": "Feature",
                "properties": {},
                "geometry": {
                    "type": "LineString",
                    "coordinates": line
                }
            }
        },
        "layout": {
            "line-join": "round",
            "line-cap": "round"
        },
        "paint": {
            "line-color": "red",
            "line-width": 13
        }
    });

getSourcegetLayer 并不意味着用于检索源数据;访问源数据的唯一方法是使用 queryRenderedFeaturesquerySourceFeatures

关于这个的文档是模糊的,但我找到了一个方法:

var feature = map.getSource('yoursourceid')._options.data;

数据源的_options.data属性保存了一个特征的等效数据。