With the code snippet below, I am aiming to utilize event data from an API to create a mapbox layer. This layer will be used to place markers and symbols on the map. However, I prefer not to use Mapbox markers as they cause issues with my code.
I have successfully accomplished this task for static data and now seek to achieve the same for dynamic data fetched from the API. I have attempted converting the generated data into GeoJSON and looping over it. But unfortunately, I encountered problems such as multiple IDs being created and code breaking.
callbackEventbrite(function(result){
console.log("env", result)
//var geo = GeoJSON.parse(result.events,{Point: [result.location.latitude, result.location.longitude]});
//console.log(geo);
const keys = Object.values(result);
for(const key of keys){
geojson = {
type: 'featureCollection',
features: [{
type: 'feature',
geometry: {
type: 'Point',
coordinates: [key.venue.longitude, key.venue.latitude]
}
}]
}
eventInfo.push(
{"longitude": key.venue.longitude , "latitude": key.venue.latitude , "name": key.name.text, "venue": key.venue.name, "date": key.start.local, "category": key.category_id}
);
}
});
function callbackEventbrite(callback){
$.ajax(briteSettings).done(function(data){
callback(data.events);
});
}
// Current code setup (static data example)
map.on("load", function(){
map.addLayer({
"id": "locations",
"type": "symbol",
"source": {
"type": "geojson",
"data": {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"Title": "The Congress Inn",
"Description": "Pub located in Longton",
"Type": "Pub",
"Address": "14 Sutherland Rd, Stoke-on-Trent ST3 1HJ",
"Longitude": 2.1316,
"Latitude": 52.9878,
"icon": "bar"
},
"geometry": {
"coordinates": [
-2.131836,
52.987238
],
"type": "Point"
}
}
Ultimately, each API result should generate its own symbol on the map using the addLayer method. Any assistance or suggestions are highly appreciated!