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

Christine Spietz spietz at fh-bingen.de
Thu May 3 02:50:31 EDT 2007


Hi Anselm,

I addded "map.zoomToMaxExtent();" at the end of my function, what should
force a repaint, but  the dynamiccaly added features are still not painted.
There also is no div or other element in front of my openlayer-map.
Unfortunately I can not test the whole thing in IE because I am working with
XForms in my form and there is a different processing of XForms in IE and
Firefox.

I have some new information which might perhaps help to solve the problem:
I made an Output of the geometry of my features with
----------------------------------->snip----------------------------------
alert("---------------------geometry.toString() = "+geometry.toString());
-----------------------------------snip>----------------------------------
Curios:
The original contained Polygons have output like:

POLYGON((3432210.443 5496303.862, 3432272.872 5496336.091, ... ))

The new added polygon behave in another way. before insertion in the layer I
also get an output of the geometry this way

POLYGON((3432210.443 5496303.862, 3432272.872 5496336.091, ... ))
but after insertion I only get an out put like
POLYGON(())

I think there is the problem. I thought that I might have to create a new
Polygon-object which could be done the following way
----------------------------------->snip----------------------------------
geometryString = geometry.toString(); //POLYGON((...))
geometryString= geometryString.substring(9,geometryString.length-2);
geometryStringArray= geometryString.split(","); //cointains the single
Points (x y)
var pointList = [];
for (i=0;i<geometryStringArray.length;i++){
    point= geometryStringArray[i];
    pointArray= point.split(" ");
    x= pointArray[0];
    y= pointArray[1];
    newPoint = new OpenLayers.Geometry.Point(x,y);
    pointList.push(newPoint);
}
linearRing = new OpenLayers.Geometry.LinearRing(pointList);
polygonFeature= new OpenLayers.Feature.Vector(new
OpenLayers.Geometry.Polygon([linearRing]));
polygonFeature.setFid(fid);
polygonFeature.setAttributes({flaeche:flaeche});
layer.addFeatures([polygonFeature]);
-----------------------------------snip>----------------------------------

But this also does not help. Besides that I also recognized, that something
might
be wrong in the geometry-object because the following code did not work:

----------------------------------->snip----------------------------------
ii = geometry.components.length;
for (i=0;i<ii;i++){
    linearRing= geometry.componentTypes[i];
    anzPoints= linearRing.components.length;
    for(a=0;a<anzPoints;a++){
        myPoint= linearRing.componentTypes[a];
    }
}
-----------------------------------snip>----------------------------------

throws an error: "linearRing.components has no properties" => There seems to
be no Point-pbjects in linearRing

So my conclusion is, that the new added features are not shown because there
seems to be something wrong with geometry.

I'm getting the geometry out of OpenLayers.Control.SelectFeature. There I
added one line of code to the select-method:

----------------------------------->snip----------------------------------
select: function(geometry) {
    //added line:
    addFeatureToLayer(geometry.feature.fid, 
geometry.feature.attributes.flaeche, geometry);
    if(geometry.feature.originalStyle == null) {
        geometry.feature.originalStyle = geometry.feature.style;
    }
this.layer.selectedFeatures.push(geometry.feature);
this.layer.renderer.drawGeometry(geometry, this.selectStyle);
this.onSelect(geometry);
},
-----------------------------------snip>----------------------------------

Any ideas what is going wrong?

Thanks for help

Christine


----- Original Message ----- 
From: "Christine Spietz" <spietz at fh-bingen.de>
To: <users at openlayers.org>
Sent: Wednesday, May 02, 2007 12:22 PM
Subject: [OpenLayers-Users] show new features (dynamically added) in
vectorlayer


> 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
>
> _______________________________________________
> Users mailing list
> Users at openlayers.org
> http://openlayers.org/mailman/listinfo/users
>




More information about the Users mailing list