I have a mapping application that lists different map layers in a menu, with each layer having a particular class name.
This class name is used when the user clicks on the name so that the layer can be either added to or removed from the map, and also the layer be added / removed from a map key.
This is working ok, but I am struggling with including a small cross next to each layer name so the user can remove them from the map key.
My code is below - I try to include an onclick function which takes the layer id and tries to use this in a function but I get an error:
Uncaught Error: Syntax error, unrecognized expression: .icon[object Object]
As my variable is now an object do I need to specify the lid
in a different way?
The html is here which shows the list option for a layer that uses the class id:
<li class='last layerlist' id='layer63'><a href='#'><i class="fa fa-circle fa-lg iconlayer63"></i><span> Flood Zone 2</span></a></li>
The Javascript below shows what happens when the user clicks on the list, it takes the id, and then uses this to get the layer name (which is the same as the id) to either hide or show the layer depending on if it is visible:
//Change layer visibility and add / remove from map key
$(".layerlist").click(function() {
var lid = $(this).attr('id');
map.getLayers().forEach(function(layer) {
if (layer.get('name') == lid) {
var visibility = layer.getVisible();
if (visibility == false) {
layer.setVisible(true);
$(".icon" + lid).removeClass("fa-circle").addClass("fa-check-circle");
$("#map-content").append("<p id='" + lid + "key'><a href='#'><i class='fa fa-times-circle' onclick='removeLayer(" + lid + ");'></i></a><img src='img/legend/" + lid + ".png' alt='Layer image'/> " + layer.get('label') + "</p>");
}
if (visibility == true) {
layer.setVisible(false);
$(".icon" + lid).removeClass("fa-check-circle").addClass("fa-circle");
$("#" + lid + "key").remove();
}
}
});
});
//Remove selected layer from map key
function removeLayer(lid) {
lid.setVisible(false);
$(".icon" + lid).removeClass("fa-check-circle").addClass("fa-circle");
$("#" + lid + "key").remove();
}
layer
object and you are passing it a string - that obviously won't work :)removeLayer
is expecting the argumentlid
to be an object:lid.setVisible(false);
Almost everything else is using it as a string, e.g.:$(".icon" + lid)...
What is thatsetVisible
line supposed to do?