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

2.9k Views Asked by At

I have 5 geojson layers - each layer have one line. I want to get the coordinates of the line with map.getSource or map.getLayer, but I don't see any geometry properites. I can get the coordinates with this:

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

My geojson is:

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
        }
    });
2

There are 2 best solutions below

0
On BEST ANSWER

The documentation is obscure regarding this but I found a way:

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

The _options.data property of the datasource holds the equivalent data of a feature.

0
On

getSource and getLayer are not meant to be used to retrieve the source data; the only way to access source data is to use queryRenderedFeatures or querySourceFeatures