Search code examples
angularopenlayers

How to remove marker by method, openlayers 4, angular 4


I want to remove the marker in Openlayers map How could i remove all the marker pin i have added to map

addmarker(name:any,lat:any,lng:any){
    console.log("inside ",lat,lng,name)
    var iconFeature = new ol.Feature({
        geometry: new ol.geom.Point(ol.proj.transform([lng,lat],'EPSG:4326', 'EPSG:3857')),
        name: name
    });
    var iconStyle = new ol.style.Style({
        image: new ol.style.Icon(({
            anchor: [0.5, 46],
            anchorXUnits: 'fraction',
            anchorYUnits: 'pixels',
            opacity: 0.75,
            src: '../../assets/images/location_pin.png'
        }))
    });
    iconFeature.setStyle(iconStyle);
    var vectorSource = new ol.source.Vector({
        features: [iconFeature]
    });
    this.vector = vectorSource
    var vectorLayer = new ol.layer.Vector({
        source: vectorSource
    });
    this.vector_layer = vectorLayer
    this.map.addLayer(vectorLayer);

}

could some one help me remove the marker pin i have added to the open layers map


Solution

  • this.map.removeLayer(vectorLayer);
    

    Yes, this means you have to keep track of all the layers you've been adding. Which sadly is the case with open layers.