I have a couple of markers that have a css class with its names, for example: markerOne has .markerone as css class, and so on.
Its possible to create a function to assign these markers to specific layers or groups? Something like var layerone = $(L.marker).hasClass("markerone"));
and assign all markers with that class inside a layer?
I want to do this so later I can toggle that layer on/off with addLayer and removeLayer.
Function that I use to show the markers:
function showResourcesByName(name) {
for (var i = 0; i < markers.resources.length; i++) {
var resName = markers.resources[i].name;
if (resName == name) {
var resIcon = icons.resources[i].icon;
var resSize = icons.resources[i].size;
var resPname = icons.resources[i].pname;
var customIcon = L.icon({
iconUrl: resIcon,
iconSize: resSize, // size of the icon
iconAnchor: [resSize[0]/2, resSize[1]/2], // point of the icon which will correspond to marker's location
popupAnchor: [2, -resSize[1]/2] // point from which the popup should open relative to the iconAnchor
});
for (var j = 0; j < markers.resources[i].coords.length; j++) {
var x = markers.resources[i].coords[j].x;
var y = markers.resources[i].coords[j].y;
marker = L.marker([y, x], {icon: customIcon});
marker.addTo(map).bindPopup(resPname);
$(marker._icon).addClass(name)
}
}
}
}
The class that I mention is the (name) part in $(marker._icon).addClass(name), which grabs the name from markers.js:
var markers = {
"resources": [
{
"name": "AITokarServer",
"coords": [
{
"x": -1210.0,
"y": -1770.0
},
{
"x": -1230.0,
"y": -1810.0
},
So all markers with the name AITokarServer will have a class .AITokarServer and so on.