To hide a feature you can give it an invisible color with rgba(0,0,0,0)
(the last value is the transparency value, 1.0 = opaque, 0 = fully transparent).
var piky1=new L.geoJson(pol00,{
style: function (feature) {
return {
fillOpacity: 0.55,
weight: 7,
color: '#eff682'
};
},
onEachFeature: function (feature, layer) {
layer.on({
'click': function (e) {
e.target.setStyle({
weight: 7,
color: 'rgba(0,0,0,0)'
});
}
});
}}).addTo(map);
Making a feature visible again if a user clicks outside of it is a bit trickier since there's no blur
event on Leaflet's layers. What you can do is listen for clicks on the map
and reset all features then:
map.on('click', function(e){
piky1.eachLayer(function (layer) {
piky1.resetStyle(layer);
});
});
Hiding individual markers it not currently supported, but should be coming in the future version 1.0 (see bug report). You can hide a marker's icon by providing an invisible icon with setIcon()
, but you can not use setIcon()
on eachLayer()
. Instead, you can use Javascript .style
properties (similar to CSS properties) on the icon to hide or show it.
// hide marker on click
layer.on('click', function(m){
m.target._icon.style.display = 'none';
m.target._shadow.style.display = 'none';
});
// show all markers again
piky3.eachLayer(function (layer) {
layer._icon.style.display = 'block';
layer._shadow.style.display = 'block';
});
Full code: http://plnkr.co/edit/rj1eF0ZhEpDGnwkz4oZr?p=preview
I think you're using the wrong tool (GeoJSON) for the job (showing markers depending on a condition). I'd approach the problem with individual markers, something like:
var map = L.map('map')
.addLayer(mapboxTiles)
.setView([42.3610, -71.0587], 4);
{% for obj in data %}
marker = L.marker([{{ obj.latitude }}, {{ obj.longitude }}] , markerOptions);
if ( obj.somethingsometing == 'visible' ) {
marker.addTo(map);
}
{% endfor %}
If you want to dinamically show/hide categories of markers, create a L.LayerGroup
per category, add the markers to a group instead of to the map, and use a L.Control.Layers
to let the user control the visibility.
Best Answer
Simply create a layer group and instead of adding tooltips directly to the map, add them to the layer group. You can then add this layer group to layer control as an overlay.