Search code examples
javagwtopenlayersgwt-openlayers

GWT openlayers, DrawFeature line style


I am using gwt openlayers to draw some linestrings on the map. I would like to change the draw feature line appearance. I noticed that PathHandler class has setStyle method, but setting style using this method does not change the line appearance.

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;
}

I was trying to set different style options but there was no effect. Does anyone know how to change appearance of DrawFeature line?


Solution

  • The handler that does the drawing (Point, Path, or Polygon) is in charge if the style of your sketches (features before they are completed).

    So to style the sketches you do :

        //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();
    

    I also added an example to she showcase : http://demo.gwt-openlayers.org/gwt_ol_showcase/GwtOpenLayersShowcase.html?example=DrawFeature%20style%20example