2013-11-23 113 views
0

我使用gwt openlayers在地图上绘制一些线条。我想改变绘制特征线的外观。我注意到PathHandler类有setStyle方法,但使用此方法设置样式不会改变线条外观。GWT openlayers,DrawFeature线条样式

private DrawFeature createDrawFeature() { 
    DrawFeatureOptions options = new DrawFeatureOptions(); 
    options.onFeatureAdded(getStyle()); 
    PathHandler handler = new PathHandler(); 
    handler.setStyle(style); 
    return new DrawFeature(layer, handler, options); 
} 
private Style getStyle() { 
    Style style = new Style(); 
    style.setStrokeColor("#ffffff"); 
    style.setStrokeWidth(2.0); 
    return style; 
} 

我试图设置不同的风格选项,但没有效果。 有谁知道如何改变DrawFeature线的外观?

回答

1

如果草图的风格(特征在完成之前)执行绘图(点,路径或多边形)的处理程序负责。

所以样式你做的草图:

//Create a style. We want a blue dashed line. 
    final Style drawStyle = new Style(); //create a Style to use 
    drawStyle.setFillColor("white"); 
    drawStyle.setGraphicName("x"); 
    drawStyle.setPointRadius(4); 
    drawStyle.setStrokeWidth(3); 
    drawStyle.setStrokeColor("#66FFFF"); 
    drawStyle.setStrokeDashstyle("dash"); 

    //create a StyleMap using the Style 
    StyleMap drawStyleMap = new StyleMap(drawStyle); 

    //Create PathHanlderOptions using this StyleMap 
    PathHandlerOptions phOpt = new PathHandlerOptions(); 
    phOpt.setStyleMap(drawStyleMap); 

    //Create DrawFeatureOptions and set the PathHandlerOptions (that have the StyleMap, that have the Style we wish) 
    DrawFeatureOptions drawFeatureOptions = new DrawFeatureOptions(); 
    drawFeatureOptions.setHandlerOptions(phOpt); 

    PathHandler pathHanlder = new PathHandler(); 

    // Create the DrawFeature control to draw on the map, and pass the DrawFeatureOptions to control the style of the sketch 
    final DrawFeature drawLine = new DrawFeature(vectorLayer, pathHanlder, drawFeatureOptions); 
    map.addControl(drawLine); 
    drawLine.activate(); 

我还添加了一个例子,她展示:http://demo.gwt-openlayers.org/gwt_ol_showcase/GwtOpenLayersShowcase.html?example=DrawFeature%20style%20example

+0

谢谢,它工作正常,但对于最后一行(我的意思是,当平局做)?它仍然有旧的薄橙色风格。 –

+0

@UP。 OK,我发现做到这一点在drawfeature选项使用onFeatureAdded方式 \t公共无效onFeatureAdded(VectorFeature vectorFeature){ \t \t vectorFeature.setStyle(对getStyle()); \t \t vectorFeature.redrawParent(); \t} –

+1

猎鹰,这可能会工作,但有点过分。 你应该在vectorlayer上使用setStyle或setStyleMap方法。这样你只需要在图层上设置一次样式。 类似于 Vector vectorLayer = new Vector(“Vector layer”); final Style drawStyle = new Style(); //创建要使用的样式 drawStyle.setFillColor(“white”); drawStyle.setGraphicName(“x”); drawStyle.setPointRadius(4); drawStyle.setStrokeWidth(3); drawStyle.setStrokeColor(“#66FFFF”); drawStyle.setStrokeDashstyle(“dash”); vectorLayer.setStyle(drawStyle); – Knarf