0
votes

Follow the example on leaflet map (http://leafletjs.com/examples/layers-control.html), I can create a layer group cities, which showed in overlayMaps Cities.

In the later stage (i.e. another function), is it possible to get the object layer group by name cities or Cities? Then I can use it in another function (e.g. leaflet-search (https://github.com/stefanocudini/leaflet-search)).

This is my pseudo code to demo my question

 find_layer_by_name = function(name){
     // add code here
 };
 var searchLayer = find_layer_by_name('cities');
 var searchcontrol = new L.Control.Search({layer: searchLayer});

Let me know if my question is not clear.

1
do you want to get cities from here var overlayMaps = { "Cities": cities }; - muzaffar

1 Answers

1
votes

If you have created an overlayMaps object with key/value pairs (as in the Leaflet example), you can use square bracket notation with your key as a string to get the corresponding layer. The following will assign cities to searchLayer:

var overlayMaps = {
  "Cities": cities
};

var searchLayer = overlayMaps["Cities"];