'Dynamically generate source data for Mapbox-GL layer

We are currently building an asset tracking widget using Mapbox-GL. When zoomed in, we show the actual assets, and this works fine. But, when zooming out, we would like to switch to a cluster view to get a proper overview of how many assets are where.

From my understanding, such layer needs to have a predefined datasource. eg. GeoJson.

Is there any way to feed Mapbox-GL data from live JS data? The assets themselves are markers, an afaik there is no way to switch from makers to something else. So I assume we would have to generate the datasource from the data that makes up the markers somehow?

The sources seems to be using URL's for getting the actual data so I'm at loss here, Can I transform a set of geo points into something that MapboxGL can use as a data source?



Solution 1:[1]

It's rather confusing what you are asking, but in general, there is no problem with updating data live.

Create the data source:

map.addSource('assets', { type: 'geojson', data: { type: 'FeatureCollection', features: [] }})

Then update it:

map.getSource('assets').setData({ ... })

I didn't understand what you mean by:

such layer needs to have a predefined datasource. eg. GeoJson.

or

The assets themselves are markers,

or

an afaik there is no way to switch from makers to something else

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1