Accessing geojson point on click is easy however it seems that mapbox does not return reference to the clicked element, it returns its copy. Probably because adding source does JSON.stringify as I believe.
I would like to access geojson specific feature on element click, modify/delete/move it and rerender.
One way of doing that is passing property "id" to feature, looping over features, find the one with proper id, modify it and rerender however that might be very slow when working with large number of points, like 100 000 of them. That is also why I am not considering usage of new Marker() as it creates DOM element and it's an overkill for big marker amounts.
Another way of doing that is probably making a map <id, FeatureObject> with references to geojson Features but I am still wondering, is there a native mapbox way of easy and performant geojson feature edition.
const geoJson = { ... }
map.addSource('points', {
type: 'geojson',
data: geoJson
});
map.addLayer({
'id': 'points',
'type': 'symbol',
'source': 'points',
'layout': {
'icon-image': 'logger_green',
'icon-allow-overlap': true
}
});
map.on('mousedown', 'points', function (e) {
e.preventDefault();
// feature here is a copy, not reference so updating geojson would require looping
// over geojson, finding clicked element by is passed in feature property, editing it and
// rerendering which may be slow for bigger geojson data
console.log(e, e.features, e.features[0]);
});