[OpenLayers-Users] limiting a Vector layer to the last feature added

Eric Lemoine eric.lemoine at camptocamp.com
Tue Aug 18 02:38:17 EDT 2009


On Tue, Aug 18, 2009 at 7:14 AM, John
Cartwright<john.c.cartwright at comcast.net> wrote:
> On Aug 16, 2009, at 11:49 AM, Eric Lemoine wrote:
>
>> Yes. I don't understand what's going on in your case. Maybe you should
>> work on a minimal example, and possibly post it here for further
>> guidance. Sorry for not being more helpful...
>
> Hi Eric,

Hi John

>
> Listed below is a example of the problem taken largely from the "Modify
> Feature" example at openlayers.org.  Thanks!

Actually the to-be-added feature is already in the layer when
beforefeatureadded triggers, so calling destroyFeatures() in a
beforefeatureadded listener also destroys the to-be-added feature .
This looks like a bug to me, so I think a ticket should be open for
this. In the mean time you can do this:

       function clearPreviousFeatures(evt) {
               var i, len, toDestroy = []
               for(i=0,len=vectors.features.length; i<len; i++) {
                   if(vectors.features[i] != evt.feature) {
                       toDestroy.push(vectors.features[i]);
                   }
               }
               vectors.removeFeatures(toDestroy);
               for(i=toDestroy.length-1; i>=0; i--) {
                   toDestroy[i].destroy();
               }
       }


Hope this helps,


PS: thanks for the example you provided.



More information about the Users mailing list