[GIS] Detecting map changes using Openlayers

javascriptopenlayers-2

Is it possible to delete a vector layer when a user pans or zooms a map? I saw in the documentation that there is a function called onMapResize. Can this also be used to detect a pan change?

I want to delete a point if the map is panned or zoomed and tried using it in the following way (which does not work):

map.OpenLayers.vectorLayer.onMapResize(console.log('map was moved'))
//use removeAllFeatures() to remove vector points

Best Answer

Vadim is right. you can do it with registering map moveend and zoomend events.

map.events.register("moveend", map, function(){
     var lyr = map.getLayersByName('vectorLayer')[0];
     map.removeLayer(lyr);
});

map.events.register("zoomend", map, function(){
     var lyr = map.getLayersByName('vectorLayer')[0];
     map.removeLayer(lyr);
});

i hope it helps you...