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

John Cartwright john.c.cartwright at comcast.net
Sat Aug 15 12:28:47 EDT 2009


Thanks for your suggestion Eric.  That was the first thing I tried,  
but it doesn't clear the map, even with an explicit redraw()

         vectorLayer.events.on({
	        "featuremodified": mapUpdated,
	        "beforefeatureadded": clearPreviousFeatures,
	        "featureadded": mapUpdated
         });


	function clearPreviousFeatures(evt) {
		vectorLayer.destroyFeatures();
		vectorLayer.redraw();
	}

	function mapUpdated(evt) {
		console.log('Vector layer has '+vectorLayer.features.length+'  
features');
		dojo.publish('mapUpdated',[evt.feature.geometry]);
	}


Setting it up this way also seems to clear the newly added feature as  
well - when the featureadded handler runs, it reports no features in  
the layer.

Is this what you had in mind?

I really appreciate your help with this.

--john


On Aug 15, 2009, at 1:26 AM, Eric Lemoine wrote:

> On Friday, August 14, 2009, John Cartwright
> <john.c.cartwright at comcast.net> wrote:
>> Hello All,
>>
>> I'm using OL 2.8 and trying to implement the behavior where the  
>> user can
>> digitize points, lines, or polygons, but only the last created  
>> feature
>> will be kept and displayed.  I've attempted this via a handler on the
>> Layer's "beforefeatureadded" event like:
>>
>> function clearPreviousFeatures(evt) {
>>    dojo.forEach(vectorLayer.features, function(f) {
>>       if (http://f.id != http://evt.feature.id) {
>>          vectorLayer.removeFeatures([f], {silent:true});
>>       }
>>    });
>>    vectorLayer.redraw();
>
> removing objects within a dojo.forEach loop may not be safe.
>
> why don't you have you beforefeatureadded listener destroy all
> features (with layer.destroyFeature)?
>
> Cheers,
>
>
>> }
>>
>>
>> However,  I find that the previous drawn features remain until the
>> DrawFeature control is changed.  Also as soon as the second feature  
>> is
>> added, the code above complains that "f is undefined".  Finally, any
>> features added subsequent to the first do not display in the style
>> defined for the layer.
>>
>> I feel like this should be a simple task and that I'm missing  
>> something
>> fundamental here.  Can someone show me what's wrong or suggest a  
>> better
>> strategy for accomplishing this goal?
>>
>> Thanks!
>>
>> --john
>>
>> _______________________________________________
>> Users mailing list
>> Users at openlayers.org
>> http://openlayers.org/mailman/listinfo/users
>>
>
> -- 
> 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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-users/attachments/20090815/d6f8eb29/attachment.html


More information about the Users mailing list