javagwtopenlayersgwt-openlayers

How can I draw a rectangle in gwt-openlayers?


I see that it is supported for the non-gwt version, here: http://openlayers.org/dev/examples/draw-feature.html (click on "draw box")

However, I can not find a way to access this function via gwt-openlayers. I am using SmartGWT, but I don't think that matters.


Solution

  • This is done using the DrawFeature control using a RegularPolygonHandler.

    See this showcase : http://demo.gwt-openlayers.org/gwt_ol_showcase/GwtOpenLayersShowcase.html?example=Draw%20Regular%20Polygon%20Example

    The draw circle button below the map is actaully a toggle button. When it is pressed in circles are drawn (which are actually polygons with 40 sides), and when it is not pressed in rectangles are drawn (which are actually polygons with 4 sides). So when the example is launched, just try to draw on the map and you will see rectangles are drawn.

    This is the code you are interested in (for creating boxes) :

    final DrawFeature drawRegularPolygon = new DrawFeature(vectorLayer, new RegularPolygonHandler());
    map.addControl(drawRegularPolygon);
    drawRegularPolygon.activate();
    RegularPolygonHandlerOptions options = new RegularPolygonHandlerOptions();
    options.setSides(4);
    ((RegularPolygonHandler) drawRegularPolygon.getHandler()).setOptions(options);