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 am trying to dynamically add features to a vector layer, and use cluster strategy to group those features accordingly.

I am able to do this for the first set of data that gets loaded on the map.

It looks like below code works only the first time - vectorLayer.addFeatures(vectordArray);

when i try to call the same addFeatures again with a new set of vectors, I dont see the new vectors. There are no errors either.

Anyone faced a situation like this? I am looking to dynamically add vectors and use CluseterStrategy.

share|improve this question
1  
is this about openlayers? i think so. pls specify this at least your tags... –  Aragon Sep 13 '12 at 13:44
    
added openlayers tag. –  Mapperz Sep 13 '12 at 13:57
add comment

2 Answers

did you try to update your vector layer after this vectorLayer.addFeatures(vectordArray) as shown at following code:

vectorLayer.redraw();

if this not work try to get all features from map into an array and then merge them with your new array then add them to map as if you are adding first time into map...

vectorLayer.removeFeatures(vectorLayer.features);
vectorLayer.addFeatures(newFeatures);

beside this checking out this example here may be helpful for you... it is working on the same principle as your will.

i hope it helps you...

share|improve this answer
    
Aragon, thanks for you response, Appreciate it. I browsed around and found a neat custom implementation here. Also, for anyone else trying this out - vectorLayer.redraw(); did not work for me. Removing the features removeFeatures and adding them again addFeatures worked, but the refresh time lag is a concern here. The custom implementation at the link is very usable for fast reCluster when adding new features. Cheers! –  Shan Sep 13 '12 at 15:49
add comment

After a bunch of experiments with API I found a way that can be used to add and remove the features on layer dynamically. But I should warn that this way is not proper according to OL API and I do not recommend to use it, because it may cause a bugs with some other OL components. For example layer.onFeatureInsert() event is not being invoken while using it.

The way is:

// to add a feature
var fId = newFeature.id;
layer.features.push(newFeature);
layer.drawFeature(newFeature);

// to remove it
featureToRemove = layer.getFeatureById(fId);  
layer.removeFeatures([featureToRemove]);
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.