[OpenLayers-Users] show new features (dynamically added) in vector layer

Christine Spietz spietz at fh-bingen.de
Wed May 2 06:22:29 EDT 2007


Hi,

I've allready posted the question at the end of April to dev at openlayers.org, 
but didn't get any answer.
So perhaps somebody in this list has an idea and can help with the following 
problem:

I have problems to show features which are dynamically added to a
vector-layer.
I first initialize my map with a baselayer and some overlays. One of the
overlays is a vector-layer which has some polygon-feature inside.

-----------------------------------snip>----------------------------------
layer_1 = new OpenLayers.Layer.Vector("layer_1");
//init the layer with
...
var polygonFeature = new OpenLayers.Feature.Vector(new
OpenLayers.Geometry.Polygon([linearRing]));
layer.addFeatures([polygonFeature]);
...
//add the layer to the map
map.addLayer(layer_1);
----------------------------------->snip----------------------------------

This works fine. Now I insert another feature in the layer for example after
a button ist clicked:

-----------------------------------snip>----------------------------------
polygonFeature = new OpenLayers.Feature.Vector(geometry);
polygonFeature .setFid(fid);
layer_1.addFeatures([polygonFeature]);
----------------------------------->snip----------------------------------

layer.features.length gives the correct length of the feature-array in
layer_1 and the geometry-object is a correct polygon. So everything seems to
be fine
Now I try to redraw the layer to show all polygon-features included in the
layer. I tried several ways but none of them worked.

-----------------------------------snip>v1-------------------------------
layer_1.redrawFeature(fid,'default');
----------------------------------->snip----------------------------------
-----------------------------------snip>v2-------------------------------
map.removeLayer(layer_1,false);
layer_1 = new OpenLayers.Layer.Vector("layer_1");
//draw the layer
map.addLayer(layer_1);
----------------------------------->snip----------------------------------
-----------------------------------snip>v3-------------------------------
drawControls = {
    polygon: new
OpenLayers.Control.DrawFeature(layer_1,OpenLayers.Handler.Polygon)
};
for(var key in drawControls) {
    map.addControl(drawControls[key]);
}
----------------------------------->snip----------------------------------

I'm using openlayers2.4 rc1 with mozilla firefox 2.0.0.3.
Can anybody please give ma a hint?

Sincerely
Christine




More information about the Users mailing list