I'm new to the Open Layers library and JavaScript in general. So I wonder why can't I specify the values of id
or style
properties of a Feature object through the opt_geometryOrProperties
constructor argument. After all it works with the geometry
property:
var g = new ol.geom.Point([0, 0]);
var feature = new ol.Feature({geometry: g});
feature.getGeometry() === g; // true
But if I try it with id
or style
, it does not work:
var g = new ol.geom.Point([0, 0]);
var id = 1;
var style = new ol.style.Style;
var feature = new ol.Feature({
geometry: new ol.geom.Point([0, 0]),
id: id,
style: style
});
feature.getId() === id; // false
feature.getStyle() === style; // false
How do I tell which properties are settable through the constructor and which are not?
I'm new to the Open Layers library and JavaScript in general. So I wonder why can't I specify the values of id
or style
properties of a Feature object through the opt_geometryOrProperties
constructor argument. After all it works with the geometry
property:
var g = new ol.geom.Point([0, 0]);
var feature = new ol.Feature({geometry: g});
feature.getGeometry() === g; // true
But if I try it with id
or style
, it does not work:
var g = new ol.geom.Point([0, 0]);
var id = 1;
var style = new ol.style.Style;
var feature = new ol.Feature({
geometry: new ol.geom.Point([0, 0]),
id: id,
style: style
});
feature.getId() === id; // false
feature.getStyle() === style; // false
How do I tell which properties are settable through the constructor and which are not?
Share Improve this question asked Feb 3, 2015 at 21:33 Levi HaskellLevi Haskell 8539 silver badges22 bronze badges2 Answers
Reset to default 3To answer your first question, you can't set the style in the constructor and it seems unlikely they are going to add it to the constructor. Concerning where you can find what properties can be set in the constructor, see the API documentation.
Something that might be useful is to set the style in the layer of the vector layer.
var id = 1;
var style = new ol.style.Style;
var feature = new ol.Feature({
geometry: new ol.geom.Point([0, 0]),
id: id,
style: style
});
For my experience this don't work realy... I use :
var feature = new ol.Feature({
geometry: new ol.geom.Point([0, 0]),
style: style
});
feature.setId(id);