[OpenLayers-Users] limiting a Vector layer to the last feature
added
Eric Lemoine
eric.lemoine at camptocamp.com
Sun Aug 16 13:49:25 EDT 2009
On Saturday, August 15, 2009, John Cartwright
<john.c.cartwright at comcast.net> wrote:
> 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?
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...
> 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
>
>
--
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