[OpenLayers-Dev] initialise layer.vector with features doesn't work

Steven M. Ottens steven at minst.net
Thu Aug 25 10:36:25 EDT 2011


Op 8/25/2011 1:51 PM, Eric Lemoine schreef:
> On Thu, Aug 25, 2011 at 12:41 PM, Steven M. Ottens<steven at minst.net>  wrote:
>> Hi all,
>>
>> I'm trying to initialise a vector layer directly with features eg:
>> var params = { features: new OpenLayers.Geometry.Point(5,52) };
>> layer = new OpenLayers.Layer.Vector('name', params);
>>
>> However, the Vector class initialises with an empty features array and
>> overrides my passed in features array, see line 267:
>> http://trac.osgeo.org/openlayers/browser/trunk/openlayers/lib/OpenLayers/Layer/Vector.js#L267
>>
>> The API docs suggest it is possible to pass in features, although doesn't
>> explicitly say so (
>> http://dev.openlayers.org/apidocs/files/OpenLayers/Layer/Vector-js.html#OpenLayers.Layer.Vector.features
>> )
>>
>> Is this a bug or a feature?
>>
>> If it is a feature what is the preferred way to initialise a vector layer
>> with a feature? Or should I first initialise the vector layer and then use
>> addFeature()?
> Hi Steven
>
> Currently addFeatures is the only way. But I agree it'd be good to be
> able to pass features to the constructor.
Hi Eric,

Thanks for the response, I did a quick hack to get it working;
Replacing
          this.features = [];
with
         if (this.features) {
             var features = this.features;
             this.features = [];
             this.addFeatures(features);
         }
         else {
             this.features = [];
         }
does the trick, however you need to pass in a rather complex thing to 
get this working:

var params = { features: new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(x,y))}

Also it is not very elegant codewise. Still it would be convenient if 
you can initialise a vector layer with features. Food for thought for OL3 ;)

Steven




More information about the Dev mailing list