In React, I'm using a pagination that returns a certain amount of results on each page. Using the address of these results, I convert them to longitude and latitude using a geo-coder package and set my markers.
setMarkers(map){
const geoCode = new GeoCode();
const dataLength = this.props.getData.length;
for(var i = 0; i < dataLength; i++) {
geoCode.geolookup(this.props.getData[i].address)
.then(result => {
// ADD A LAYER EACH TIME AN ADDRESS IS CONVERTED TO
// LONGITUDE AND LATITUDE
map.addLayer(new VectorLayer({
source: new VectorSource({
features: [new Feature({
geometry: new Point(fromLonLat([result[0].lng, result[0].lat])),
name: 'Marker',
id: 'this.props.getData[i].id_number'
})],
}),
name: 'Marker', // THE NAME OF THE VECTOR LAYER
zIndex: 5
}))
})
}
this.mapClickHandler(map)
}
However, each time the user goes to a new page, all the marker layers should be removed so that I can add new marker layers based on the new results.
I know that I have to remove layers like this:
map.removeLayers()
But how do I use this to remove layers based on the layer name value 'Marker'
map.getLayers().forEach(function(layer) { if (layer.get('name')=='Marker') map.removeLayer(layer); });