[GIS] Clear markers from OSM map with openlayers

openlayers-2openstreetmap

I have a bunch of Point data that refreshes every few minutes. To render these points, I have created the markers as OpenLayers.Feature.Vector Objects and added them to my Vector layer. The layer gets added to the map object, and I can see the markers when the page loads.

When the data refreshes on my local server, some points from the previous dataset may have disappeared and some new points may have been added. I am just given a large list of points to render every refresh.

Because of this, I need to clear the vector layer of all current markers and then re-render the map with the new dataset.

Also, is there a way to delete specific markers given just a lat,lng? Or is there some way to reference them and delete it from the vectory layer?

How do I clear the vector layer of all markers and how do I delete specific points?

Best Answer

Clearing is done with removeAllFeatures:

pointLayer.removeAllFeatures();

You can remove any that you've stored with removeFeatures:

var aFeature;
//set aFeature somehow 
pointLayer.removeFeature([aFeature]);

If you need to find them by location (lat long) I'd look into selecting the features with a spatial filter then removing them with removeFeatures.