Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have an OpenLayers layer with several vectors on it. I know the OpenLayers Feature ID (e.g. OpenLayers.Feature.Vector_241) and I would like to know how to remove this feature and only this feature?

I have considered deleting them all and then looping through an array to redraw them, but this seems a little inelegant, and long winded.

I have tried a variety of versions of the folllowing code:

layer.removeFeatures( featureID );

And

layer.removeFeatures( [featureID] );

And

layer.removeFeatures( {feature : featureID });

Etc...

All help is welcome!

Thanks

C

share|improve this question
add comment

2 Answers 2

up vote 14 down vote accepted

I don't think that OpenLayers is capable of removing a feature by providing the featureID. It seems like it can only remove a features by providing a feature or an array of features:

layer.removeFeatures(featureObject);

But you could instead locate the feature first and then remove the found feature:

layer.removeFeatures(layer.getFeatureById(featureID));

Or similar if its not the OpenLayers FeatureID you have, then

layer.removeFeatures(layer.getFeatureBy('myId', myID));
share|improve this answer
    
I like the thinking! The problems is though that the "layer.getFeatureById(featureID))" statement returns null, and so the removeFeature() doesnt work. Im 100% that i have the right FeatureIDs though.... any more thoughts? –  CatchingMonkey Sep 13 '11 at 14:23
    
Scratch that. I was using the wrong featureID. Im using your second example and it works perfectly. Nice one! –  CatchingMonkey Sep 13 '11 at 14:43
add comment

This worked for me:

*layer.removeFeatures(featureObject);*

But i've got the feature as a global variable, maybe in other cases you should try to keep the feature ID in order to search to remove it.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.