0

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.

4

1 回答 1

1

尝试使用"boxselectionend"SelectFeature 控件的事件(需要 2.12)

但是这个事件不返回边界或所做的选择,只返回一个图层数组。

另一种选择是在外部创建 Handler.Box,这就是我在某些情况下所做的:

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 ...
        }
    },
    {}
);
于 2013-03-18T22:01:34.257 回答