最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to initialize ol.Feature object's id or style property from the constructor - Stack Overflow

programmeradmin0浏览0评论

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 badges
Add a ment  | 

2 Answers 2

Reset to default 3

To 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);
发布评论

评论列表(0)

  1. 暂无评论