How do I group all the different L.geoJSON queries under a single L.control layerGroup? I can get it to work if the control shows every single queried L.geoJSON but when I try to group them it fails and when you toggle it displays nothing. When I add pointGroup = L.layerGroup... and CustomLayer.addOverlay... under pointCoordinates.push(geoJsonLayer) it displays the layers but each one is toggle-able.
jQuery.ajax({
type: "GET",
dataType: "json",
url: "https://api.weather.gov/alerts?active=1&event=Red%20Flag%20Warning",
error: function (err) { console.log(err)},
success: function (data, status, xhr) {
var pointCoordinates = [];
var pointGroup;
jQuery(data.features).each(function(index, el) {
var zones = el.properties.affectedZones;
// Iterate through each zone
for (var i = 0; i < zones.length; i++) {
jQuery.ajax({
type: "GET",
dataType: "json",
url: zones[i],
error: function (err) { console.log(err)},
success: function (results, status, xhr) {
var geoJsonLayer = L.geoJson(results, {
style: function (feature) {
return {color: '#ed1a39', weight: '1'};
}
});
pointCoordinates.push(geoJsonLayer);
}
})
}
});
pointGroup = L.layerGroup(pointCoordinates).addTo(map);
CustomLayer.addOverlay(pointGroup, "Red Flag Warning(s)");
jQuery("#red-flag-events").html(data.features.length);
}
})
Map Javascript:
jQuery(document).ready(function($) {
var point = [];
var pointGroup;
L.mapbox.accessToken = 'pk.eyJ1IjoidGhlcHJlcGFyZWRsaWZlIiwiYSI6ImNqZzh3ZWptOTI3ZWozM2xvcjY1YzFtNHUifQ.UchKM7tZByT6mZTxqMlaSQ';
map = L.mapbox.map('map', 'mapbox.light')
.setView([40, -100], 4)
.addControl(L.mapbox.geocoderControl('mapbox.places', {
keepOpen: false
}));
var overlays = {
};
CustomLayer = L.control.layers(null, overlays, { collapsed: false }).addTo(map);
fnWildlandFires();
});
Unfortunately no data is passed when I group them under pointGroup and try to pass it as a L.layerGroup.