0
votes

I have an OpenLayers map and I want users to be able to draw a box by dragging their mouse (similar to this example here, select the "select feature (0 features selected)" option first) and obtain the boundaries of the drawn box.

I can manage to draw the box using smth like below, however it won't work when there are no features in the map or no features selected, and that will certainly be the case.

new OpenLayers.Control.SelectFeature(this._layers.osm, {
        multiple: true,
        box: true, 
        hover: false, 
        toggleKey: 'ctrlKey', 
        multipleKey: 'shiftKey',
        onBeforeSelect: function() {
            console.log(arguments);
        }
    })

Is there an easy way to accomplish this in OpenLayers or should I do the heavy lifting myself by tracking mouse drags and drawing/removing polygons accordingly?

Thanks.

1

1 Answers

1
votes

Try to use "boxselectionend" event of SelectFeature control (requires 2.12)

But this event not returns boundaries or the selection made, only returns a layers array.

Another option is to create the Handler.Box externally, that is what I do in some cases as:

var mySelectFeature = OpenLayers.Control.SelectFeature(...);
var myHandlerBox = new OpenLayers.Handler.Box(
    mySelectFeature, {
        done: function(bounds) {
            OpenLayers.Control.SelectFeature.prototype.selectBox.apply(
                              mySelectFeature, arguments);
            ... your code ...
        }
    },
    {}
);