Search code examples
javascripthtmlleafletmarkers

How to delete my map markers in javascript


I just can't delete my markers. I have tried all the pages here already. But it seems my knowledge is too limited.

Is the problem that I don't add the markers to a layerGroup the right way?

var markers = [];
var layerGroup;

function makeMap(inputData){
  layerGroup = L.layerGroup(markers).addTo(map);
}

function handleLayer(layer){
  markers.push(L.marker(layer.feature.properties.koordinater, {icon: L.divIcon({ className: 'css-icon', html: value, iconSize: [50, 1] })}));
}

function deleteMarkers(){       
  layerGroup.clearLayers(map);
}

Solution

  • You are calling the wrong function in deleteMarkers. This should help:

    function deleteMarkers() {
        layerGroup.clearLayers();
    }
    

    EDIT

    According to the comments in the comment section layerGroup.clearLayers() solved the problem.