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

Eric Lemoine eric.lemoine at camptocamp.com
Wed Aug 19 01:22:33 EDT 2009


On Wednesday, August 19, 2009, John Cartwright
<john.c.cartwright at comcast.net> wrote:
>
> On Aug 18, 2009, at 12:38 AM, Eric Lemoine wrote:
> Hi John
>
> 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.
>
> Thanks Eric, this works great!
> I notice that there doesn't seem to be any difference between this:	vectors.removeFeatures(toDestroy);	for(i=toDestroy.length-1; i>=0; i--) {		toDestroy[i].destroy();	}
> and this:
> 	vectors.destroyFeatures(toDestroy);
> Is that what you'd expect?

You are correct. And using destroyFeatures is better IMO - less code
on the application side.

Cheers,

-- 
Eric Lemoine

Camptocamp France SAS
Savoie Technolac, BP 352
73377 Le Bourget du Lac, Cedex

Tel : 00 33 4 79 44 44 96
Mail : eric.lemoine at camptocamp.com
http://www.camptocamp.com



More information about the Users mailing list