OpenLayers OpenLayers

Ticket #761 (closed bug: fixed)

Opened 1 year ago

Last modified 1 year ago

Vector Features and Layer Destruction

Reported by: euzuro Assigned to:
Priority: minor Milestone: 2.5 Release
Component: general Version: 2.4
Keywords: Cc:
State:

Description

As reported to the users@ mailing list:

> >I'm having trouble using and then removing a vector layer.
> >removeFeatures() - produces errors saying that the feature is not
> >defined when feature.geometry is referenced.
>
> map.layers[1].removeFeatures(map.layers[1].features); works for me on
> http://openlayers.org/dev/examples/vector-features.html .
>
> >calling destroy on the layer causes continuous mouse event errors
> >- it seems that the getFeatureFromEvent method keeps getting called
> >even though I've destroyed the layer.
>
> Yep. That's a bug. The only piece of advice I have for that is to not
> destroy a layer if you have selectefatures handler on it :) I'll file it
> as a bug.

Attachments

vector.patch (1.5 kB) - added by crschmidt on 07/15/07 01:04:13.

Change History

06/13/07 16:04:07 changed by euzuro

Suggestions from Mike Q (also from users@ list):

OpenLayers.Control.EraseFeatures = OpenLayers.Class.create();
OpenLayers.Control.EraseFeatures.prototype =
 OpenLayers.Class.inherit( OpenLayers.Control, {
   /** @type OpenLayers.Control.TYPES */
   type: OpenLayers.Control.TYPE_BUTTON,
   trigger: function () {
     if (this.map) {
       var map = this.map;
       for(i = 0; i < map.layers.length; i++) {
         var currentLayer = map.layers[i];
         if (currentLayer.isVector) {
           if (currentLayer.selectedFeatures.length > 0) {
         currentLayer.destroySelectedFeatures();
       } else {
         currentLayer.destroyFeatures();
       } //if (currentLayer.selectedFeatures...
         } //if (currentLayer.isVector...
       } // for (i = 0...
     } //if (this.map...
   },
/** @final @type String */
CLASS_NAME: "OpenLayers.Control.EraseFeatures"
});

and

Also, add the following to OpenLayers.Layer.Vector:

   destroySelectedFeatures: function () {
       for (var i = (this.selectedFeatures.length - 1); i >= 0; i--) {
           var selectedFeature = this.selectedFeatures[i];
           this.selectedFeatures = OpenLayers.Util.removeItem(this.selectedFeatures, selectedFeature);
           this.renderer.eraseGeometry(selectedFeature.geometry);
       }
   },

07/15/07 01:04:13 changed by crschmidt

  • attachment vector.patch added.

07/15/07 01:06:23 changed by crschmidt

  • keywords set to review.

Seems like Mike Q's comments are helpful, but not solving the problem that the user is interested in -- specifically, in having the ability to destroy a layer without getting errors from a select handler.

What should be done at the application level is to turn off -- deactivate or destroy -- the selectfeature handler before destroying the layer. However, in order to prevent shooting oneself in the foot, we can also put a safety catch with a Console.error into getFeatureFromEvent. The attached patch does this, with a test.

09/11/07 15:10:43 changed by elemoine

crschmidt's patch looks good to me

09/11/07 15:19:38 changed by crschmidt

  • keywords deleted.
  • status changed from new to closed.
  • resolution set to fixed.

(In [4220]) Fix for getFeatureFromEvent method on destroyed layer. (Closes #761) Review by elem (yay)