在打开的图层中更改矢量要素的绘图属性

时间:2012-11-28 19:39:57

标签: javascript openlayers

我从服务器加载shapefile,而不是在OpenLayers中绘制它。 shapefile包含超过400,000个具有不同不透明度的多边形。我需要设置不透明度和填充颜色,但openlayers似乎忽略它,而只是绘制橙色方块。我更改属性之前是console.log(),然后它显示我分配的内容。谁能告诉我它为什么这样做?

var green = {
    fill: true,
    fillColor: "#006633",
    fillOpacity: 1
};

var features = wkt.read(element);
if (featureNumber == 0){
 document.getElementById('result').innerHTML=element;
}
features = element.toString();
var bounds;
var b = features.indexOf('MULTIPOLYGON', 0);


var c = features.indexOf('MULTIPOLYGON', 40);
if (c == -1) {
    c = element.indexOf(':',b+1);
}

leftovers = features.substring(c,100000000000000000);

features = features.substring(b,c);


features = wkt.read(features);

if(features) {
    if(features.constructor != Array) {
        features = [features];
    }
    for(var i=0; i<features.length; ++i) {
        if (!bounds) {
            bounds = features[i].geometry.getBounds();
        } else {
            bounds.extend(features[i].geometry.getBounds());
        }

    }

    pointLayer.addFeatures(features);
    console.log(pointLayer.features[featureNumber].attributes );

    pointLayer.features[featureNumber].attributes = green;
    console.log(pointLayer.features[featureNumber].attributes );

    featureNumber++

     map.zoomToExtent(bounds);
    var plural = (features.length > 1) ? 's' : '';
    console.log('Feature' + plural + ' added');
    console.log('feature number: '+featureNumber)

    if (leftovers.indexOf('MULTIPOLYGON',0) != -1) {
        parseWKT(leftovers,shapefile);
    }

} else {
   final(leftovers, shapefile);
}

}

1 个答案:

答案 0 :(得分:4)

该样式属于.style属性,而不属于该功能的.attributes。如果已经在地图上,您还需要致电redraw()

pointLayer.features[featureNumber].style = green;
pointLayer.redraw();

如果您想从默认样式开始,只需更改一些内容,您可以执行以下操作:

var green = OpenLayers.Util.applyDefaults(green, OpenLayers.Feature.Vector.style['default']);
green.fill = true;
green.fillColor = "#006633";
green.fillOpacity = 1;
pointLayer.features[featureNumber].style = green;
pointLayer.redraw();