矢量特征重新渲染

时间:2011-07-19 16:19:03

标签: javascript openlayers

在OpenLayers中,我有一个LineFeature对象。

我改变了对象的样式(例如my_line.style.strokeColor="Black")。

但是在我放大/缩小(即重新渲染)之前,屏幕上的颜色不会改变。

如何判断重新渲染所需的矢量要素?

2 个答案:

答案 0 :(得分:2)

在更改样式后,尝试在包含该功能的矢量图层上调用redraw()方法。

更新:如果不想重绘整个图层,可以尝试使用drawFeature()方法。如果你看一下它的代码,你可能会更好地了解它是如何工作的:

/**
     * APIMethod: drawFeature
     * Draw (or redraw) a feature on the layer.  If the optional style argument
     * is included, this style will be used.  If no style is included, the
     * feature's style will be used.  If the feature doesn't have a style,
     * the layer's style will be used.
     * 
     * This function is not designed to be used when adding features to 
     * the layer (use addFeatures instead). It is meant to be used when
     * the style of a feature has changed, or in some other way needs to 
     * visually updated *after* it has already been added to a layer. You
     * must add the feature to the layer for most layer-related events to 
     * happen.
     *
     * Parameters: 
     * feature - {<OpenLayers.Feature.Vector>} 
     * style - {String | Object} Named render intent or full symbolizer object.
     */
    drawFeature: function(feature, style) {
        // don't try to draw the feature with the renderer if the layer is not 
        // drawn itself
        if (!this.drawn) {
            return;
        }
        if (typeof style != "object") {
            if(!style && feature.state === OpenLayers.State.DELETE) {
                style = "delete";
            }
            var renderIntent = style || feature.renderIntent;
            style = feature.style || this.style;
            if (!style) {
                style = this.styleMap.createSymbolizer(feature, renderIntent);
            }
        }

        var drawn = this.renderer.drawFeature(feature, style);
        //TODO remove the check for null when we get rid of Renderer.SVG
        if (drawn === false || drawn === null) {
            this.unrenderedFeatures[feature.id] = feature;
        } else {
            delete this.unrenderedFeatures[feature.id];
        };
    }

答案 1 :(得分:0)

嗨,你可以这样做:

....
map.vectorlayer.addFeatures(line);
var mystyle = OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style['default']);
mystyle.strokeColor = '#7200FF';
...
map.vectorlayer.styleMap.styles['default'].defaultStyle = mystyle;
map.vectorlayer.redraw(true);