切换填充Openlayers样式中的颜色

时间:2012-10-10 04:42:31

标签: openlayers

我想在Openlayers中为特定图层切换一个点的fillColor?有可能吗?眨眼之类的东西?

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

无效

1 个答案:

答案 0 :(得分: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);