5

I have a simple Marker added to map like this:
var x = L.marker([50.5, 30.5]); x.addTo(map);

Now how can I hide/remove x? I searched the API docs for a Marker.remove() method, but couldn't find any.

Even more odd - Google is failing me. All results I can get is how to remove the whole layer with all markers, but not only one.

Am I getting something wrong here about markers? Is there a way to remove only one?

8

Yes, it might sound confusing but internally markers are treated like layers. You have to use removeLayer, it will still only remove a single marker:

var x = L.marker([51.5, -0.09]).addTo(map);
var y = L.marker([51.45, -0.09]).addTo(map);
map.removeLayer(x);
0

First, adding a lot of markers:

for (var i=0; i < schools.length;i++){
    var marker = L.marker([schools[i].latLng.lat, schools[i].latLng.lng], {
          icon: L.mapbox.marker.icon({
            'marker-color': '#9c89cc'
          })
        })
        .bindPopup('<div class=\"marker-title\"><h2>'+schools[i].uniName+'</h2></div>')
        .addTo(map);
    allMarkers.push(marker);
  }

Then hiding that specific marker (in this case I just hide the first, but you could hide based on any number of variables):

$(allMarkers[0]._icon).hide()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.