2013-11-23 65 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