简体   繁体   English

OpenLayers 3:如何设置矢量要素的填充样式

[英]OpenLayers 3: how to set fill style of a vector feature

i am trying to set the fill colour of seperate features of a vector layer. 我正在尝试设置矢量图层的单独特征的填充颜色。 using the code below, i thought i would be able to iterate through the features and set their fill style individually, but a strange issue happens. 使用下面的代码,我想我将能够遍历这些功能并单独设置它们的填充样式,但是会出现一个奇怪的问题。 Without the setStyle function, the various properties of the features are logged in the console. 如果没有setStyle函数,则会在控制台中记录功能的各种属性。 id, name and geometry. id,名称和几何。 There is about 5 or so features that get logged. 大约有5个左右的功能被记录下来。 basically like 基本上就像

room1
room2
room3
room4
room5

with the extra data underneath each one (id, geometry) 每个下面的额外数据(id,几何)

but when i add the line for setting the fill of the feature, i get a strange problem. 但是当我添加用于设置功能填充的行时,我遇到了一个奇怪的问题。 It seems to hang the loop on the first feature and the console fills up with logs of that features properties, like: 它似乎将循环挂在第一个功能上,并且控制台填满了该功能属性的日志,例如:

room1
room1
room1
room1
room1
room1
room1

for a long time, to the point where the firefox log limit is reached and it tells me that 2000 entries are not shown! 很长一段时间,到达firefox日志限制的点,它告诉我没有显示2000条目!

but on the plus side, the first feature does actually get its fill colour changed! 但从好的方面来说,第一个功能实际上确实改变了它的填充颜色! so i think the line of code i used is at least half right! 所以我认为我使用的代码行至少是一半! but there is definately something drastically wrong with it. 但肯定存在一些严重错误。

the code: 编码:

vector.getSource().on('change', function (evt) {
    var source = evt.target;
    if (source.getState() === 'ready') {

        var features = vector.getSource().getFeatures()
        for (var k in features) {
            console.log(features[k].getProperties()['name']);
            console.log(features[k].getProperties()['id']);
            console.log(features[k].getGeometry()['n']);
            features[k].setStyle(new ol.style.Style({fill: fill}));
        }

    }        
});

i really do not know much about OL3 or styling features and i arrived at this through a lot of trial and guessing. 我真的不太了解OL3或造型功能,我通过大量的试验和猜测来达到这个目的。 can anyone point me in the right direction? 任何人都能指出我正确的方向吗?

So, here is your plunk modified. 所以,这是你的修改。

First of all, unless you have a specific reason, use the latest library version. 首先,除非您有特定原因,否则请使用最新的库版本。 This is the main reason your kml was not loading. 这是你的kml没有加载的主要原因。

And secondly, your setFill became this: 其次,你的setFill成了这样:

    vector.getSource().forEachFeature(function(feature){

        console.log(feature.getProperties());

        style = new ol.style.Style({
            //I don't know how to get the color of your kml to fill each room
            //fill: new ol.style.Fill({ color: '#000' }),
            stroke: new ol.style.Stroke({ color: '#000' }),
            text: new ol.style.Text({
                text: feature.get('name'),
                font: '12px Calibri,sans-serif',
                fill: new ol.style.Fill({ color: '#000' }),
                stroke: new ol.style.Stroke({
                    color: '#fff', width: 2
                })
            })
        });
        feature.setStyle(style);
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM