Reputation: 418
This is a dumb question, in my leaflet/javascript project i have a hundred layers, (i have not created, however I have to work on it), divided between LayerGroup, FeatureGroup, MarkerCluster and GeoJSON, I need to create a single layer root that contains all the information for make some research and then once I have found the information, i want to retrieve the single layer containing the information . So to make the merge layer i do the following code:
//My 100 Layer....
var library = new L.MarkerClusterGroup(/*some layer options*/);
var restaurant = new L.FeatureGroup(/*some layer options*/);
var house = new L.LayerGroup(/*some layer options*/);
var notClassified = new L.geoJson(content,/*some layer options*/);
//......
So now i want to merge all this Layers to a unique Layer, L.geoJson and L.MarkerClusterGroup extends the L.FeatureGroup, the L.FeatureGroup extends L.LayerGroup, so the root Layer must be a L.LayerGroup unless someone has a better advice:
var worldLayer = new L.LayerGroup(/*some layer options*/);
for(var layer in listOfAllLayers){
worldlayer.addLayer(layer);
}
map.addLayer(worldLayer);
Optional Question: is this the best way to do this?
After created my worldLayer i make some research, e.g. track the id assigned to a specific L.Marker or a specific Json Object on the GeoJson of worldlayer. So i think the best way is work on the GeoJson result of the WorldLayer:
//Get json content from the worldlayer Layer
var json = worldLayer.toGeoJSON();
var key =...,
var value = ..-;
for(var i =0; i < json.features.length; i++){
var resulJson = searchJsonByKeyAndValue(json.features[i],key,value);
//now work with resultJson....
}
//key is the id and the value is what i looking for e.g. 456
function searchJsonByKeyAndValue(json,key,value){
for (var i = 0; i < json.length; i++) {
try {
if (json[i].hasOwnProperty(key)) {
if (json[i][key] == value) {
return json[i];
}
}
}catch(e){
alert(e.message);
}
}
return null;
}
Optional Question 2: is this the best way to do this?
My question: among other information, there is a way to find the specific original layer of the json object information?
For example the 'worldLayer', I know that specific information is present also in 'libraries', so i want to retrieve the value "libraries" or the object Layer libraries.
Now I solved by adding to each Feature of the various layers a further information in the Feature json object call "originLayers", so when I recover the json object with the procedure I have illustrated, I recover even the desired information; but for to do this I have to go to analyze the GeoJSON of all layers (not really small), and for each single feature add the new info. There is an alternative way to improve everything?
Thank you in advance for any help.
Upvotes: 0
Views: 476
Reputation: 1395
I think the hasLayer method of layerGroup is what you need instead of your pseudo-retriever function to keep track of marker.
For example :
var map = map();
var a = layerGroup().addTo(map);
var b = featureGroup().addTo(a);
var c = featureGroup().addTo(a);
var marker = marker().addTo(b);
a.hasLayer(marker); // true
b.hasLayer(marker); // true
c.hasLayer(marker); // false
Upvotes: 1