Search code examples
vectoropenlayerslimitlayer

OpenLayers Vector Layer Maximum number of features


I was wondering if it is possible to limit the number of features you an draw on a vectorylayer. If I already have a polygon drawn, and go to draw another it will delete the first feature.

If not is it possible to add a listener to the vector layer, so that I can erase all features when clicking to draw?

Thanks!!!


Solution

  • You can't set max number of features on Vector Layer, but it has a whole lot of events where you can inject your own logic. Here are available events:

    "beforefeatureadded", "beforefeaturesadded",
    "featureadded", "featuresadded", "beforefeatureremoved",
    "beforefeaturesremoved", "featureremoved", "featuresremoved",
    "beforefeatureselected", "featureselected", "featureunselected", 
    "beforefeaturemodified", "featuremodified", "afterfeaturemodified",
    "vertexmodified", "vertexremoved", "sketchstarted",
    "sketchmodified", "sketchcomplete", "refresh"
    

    You can use "beforefeatureadded" for instance:

    your_vectror_layer.events.on({'beforefeatureadded': function(){
      //Remove first feature or whatever you want to do
    }});