Vector Feature Rerender
In OpenLayers I have a LineFeature object.
I change the 开发者_C百科style of the object (e.g. my_line.style.strokeColor="Black"
).
But the colour doesn't change on the screen until I zoom in/out (i.e. rerender).
How do I tell the vector feature it needs to rerender?
Try calling redraw() method on vector layer that contains the feature after you change style.
UPDATE: If don't want to redraw whole layer you can try drawFeature() method. You'll probably have a better idea how it works if you take a look at it's code:
/**
* 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];
};
}
hi you can do this as below:
....
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);
精彩评论