How to get raw geojson data from addSource request
Hello - I'm working through the "Build a store locator using Mapbox GL JS" tutorial for building a map using Sweetgreens data:
https://docs.mapbox.com/help/tutorials/building-a-store-locator/
However, instead of hardcoding the data directly in the javascript file, I would like to store it on the server and then load it via the AddSource() method. That part works fine, but then it's unclear how to pass the raw data into the buildLocationList() function as defined in the link above?
More specifically, I have the following code:
map.on('style.load', function() {
map.addSource("geo_data", {
"type": "geojson",
"data": "/data.geojson",
"buffer": 1,
"maxzoom": 14,
"generateId": true
});
map.addLayer({
id: 'locations',
type: 'symbol',
source: "geo_data",
layout: {
'icon-image': 'restaurant-15',
'icon-allow-overlap': true,
}
});
buildLocationList(geo_data)
which loads the sweetgreens geojson from the server (that part works fine), but it's unclear how to pass the raw data into the buildLocationList() function, which was defined in the tutorial:
I realize that the addSource function is loading the geojson via a background thread, so I would prefer to stay in the library and not use an external library for loading the raw data. I'm looking for something like this: geoJsonData = map.getData("geo_data")
I did try the getSource method: map.getSource("geo_data") but I couldn't find the raw data :(
Thanks!
This looks like something that could be achieved with map.querySourceFeatures: https://docs.mapbox.com/mapbox-gl-js/api/#map#querysourcefeatures
@vgoklani can you confirm you were trying to query data from tiles that you expect was loaded in the map view. If the tiles with the data you wanted to fetch was outside the map view, you can expect no results.
Array
Alternatively, you can load the GeoJSON and process it on the app side in parallel to adding the source. Since browser would cache it, you won't download it twice.
Hello - thanks for the follow-up. I tried this:
map.on('load', function() {
map.addSource("geo_data", {
"type": "geojson",
"data": "/data.geojson",
"buffer": 1,
"maxzoom": 14,
"generateId": true
});
map.addLayer({
id: 'locations',
type: 'symbol',
source: "geo_data",
layout: {
'icon-image': 'restaurant-15',
'icon-allow-overlap': true,
}
});
const stores = map.querySourceFeatures("geo_data");
console.log(stores)
but it's unfortunately returning an empty array.
@mourner is the GeoJSON data stored somewhere(?) even as a private variable?
@planemad the data is visible in the default map - the code is taken from the sweetgreens example, however I'm just passing the data from the server instead of hard-coding it. Thanks!
@vgoklani Just a comment.
but it's unfortunately returning an empty array.
If you get the Feature data by querySourceFeatures, you probably should wait until the datasource was loaded.
map.on('sourcedata', (e) => {
if(e.sourceId === "geo_data" && e.isSourceLoaded){
const stores = map.querySourceFeatures("geo_data")
console.log(stores)
}
})
@vgoklani Just a comment.
but it's unfortunately returning an empty array.
If you get the Feature data by querySourceFeatures, you probably should wait until the datasource was loaded.
map.on('sourcedata', (e) => { if(e.sourceId === "geo_data" && e.isSourceLoaded){ const stores = map.querySourceFeatures("geo_data") console.log(stores) } })
Hey, I tried doing this too. But still its returning an empty array.
I got the same result, an empty array. But I just didn't add the data on the map itself (I used a layer myself). Once I did, it started to return results in the given array.