I have about 50 different map layers to be added on a map. Each map layer data is stored as separate JS file (lay1.js, lay2.js, ...... lay50.js). Each layer have its own different properties also like Some layers may be polygons, some may be markers etc. My code is almost like as given below. Is there any way to add these .js geojson data files dynamically when the user click its corresponding layer checkbox instead of loading all as script files? Also i would like to know how to call the layers dynamically instead of storing each layer in separate variables as seen in code below.
var layer1 = L.geoJSON([lay1], {style: function (feature) {}, onEachFeature: onEachFeature});
Please Help. My full code is given below.
<script src="lay1.js" type="text/javascript"></script>
<script src="lay2.js" type="text/javascript"></script>
..............
..............
<script src="lay50.js" type="text/javascript"></script>
<input type="checkbox" id="1" onClick="togglejsonLayer(this);"/> Layer 1
<input type="checkbox" id="2" onClick="togglejsonLayer(this);"/> Layer 2
..............
..............
<input type="checkbox" id="50" onClick="togglejsonLayer(this);"/> Layer 50
<div id="map" style="height:600px; width:100%;"></div>
<script>
mbAttr = '',
mbUrl = 'https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token=xxxxxxxxxxxxx';
var streets = L.tileLayer(mbUrl, {id: 'mapbox/streets-v11', tileSize: 512, zoomOffset: -1, attribution: mbAttr}),
sattelite = L.tileLayer(mbUrl, {id: 'mapbox/satellite-v9', tileSize: 512, zoomOffset: -1, attribution: mbAttr});
var map = L.map("map", {
center: [8.4819, 77.0361],
zoom: 12,
layers: [streets]
});
var baseLayers = {
"Streets": streets,
"Sattelite": sattelite
};
L.control.layers(baseLayers).addTo(map);
function onEachFeature(feature, layer) {
var popupContent;
if (feature.properties && feature.properties.popupContent) {
popupContent = feature.properties.popupContent;
}
layer.bindPopup(popupContent);
}
var layer1 = L.geoJSON([lay1], {style: function (feature) {}, onEachFeature: onEachFeature});
var layer2 = L.geoJSON([lay2], {style: function (feature) {}, onEachFeature: onEachFeature});
..............
..............
var layer50 = L.geoJSON([lay50], {style: function (feature) {}, onEachFeature: onEachFeature});
});
var layers = [layer1, layer2, .............. ,layer50];
selId = null;
function togglejsonLayer(checkbox) {
var checkId = checkbox.id;
if (checkbox.checked) {
layers[checkId - 1].addTo(map);
selId = checkId;
}
else {
map.removeLayer(layers[checkId - 1]);
selId = null;
}
}
</script>