1

我想为 Openlayers 中的特定图层切换点的填充颜色?有可能这样做吗?眨眼之类的?

fillColor:${getFillColor},
context : {
  getFillColor : function(feature) {
    if (feature.data.fillColor == 'red') {
      return 'yellow';
    } else {
      return 'red';
    }

这不起作用

4

1 回答 1

3

即使没有基于上下文的样式,您也应该能够完成该任务;您只需要定期更改矢量图层的样式并重新绘制它。尝试类似:

window.setInterval(function (){
    var defaultStyle = yourVectorLayer.styleMap.styles['default'].defaultStyle;
    yourVectorLayer.styleMap.styles['default'].defaultStyle = {
        fillColor: defaultStyle.fillColor === 'blue' ? 'green' : 'blue',
        pointRadius: 10
    }
    yourVectorLayer.redraw();
}, 1000);

一个完整的工作示例如下

var map = new OpenLayers.Map('map', {
    maxResolution:'auto',
    layers: [
        new OpenLayers.Layer.WMS( 
            "OpenLayers WMS",
            "http://vmap0.tiles.osgeo.org/wms/vmap0",
            {layers: 'basic'}
        ),
        new OpenLayers.Layer.Vector('Points', {
            styleMap: new OpenLayers.StyleMap({
                pointRadius: 10, 
                fillColor: "blue"
            })
        })
    ],
    center: [0,0]
});

var features = [];
for (var i=0; i<100; i++) {
    var x = Math.random() * 360 - 180,
        y = Math.random() * 180 - 90;
    features.push(
        new OpenLayers.Feature.Vector(
            new OpenLayers.Geometry.Point(x, y)
        )
    );
}

map.layers[1].addFeatures(features);

window.setInterval(function(){
    var defaultStyle = map.layers[1].styleMap.styles['default'].defaultStyle;
    map.layers[1].styleMap.styles['default'].defaultStyle = {
        fillColor: defaultStyle.fillColor === 'blue' ? 'green' : 'blue',
        pointRadius: 10
    }
    map.layers[1].redraw();
}, 1000);
于 2012-10-10T08:15:29.497 回答