2013-03-18 85 views
0

我有一个OpenLayers地图,我希望用户能够通过拖动鼠标来绘制框(类似于this example here,先选择“选择功能(选定0个功能)”选项)并获得绘制框的边界。从OpenLayers中的选择框中获取框边

我可以使用像下面这样的smth来绘制箱子,但是当地图中没有任何特征或没有选择特征时,它将不起作用,那肯定会是这种情况。

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

是否有一个简单的方法的OpenLayers做到这一点,或者我应该做繁重的通过跟踪鼠标拖动和绘图/删除相应的多边形提升自己?

谢谢。

回答

1

尝试使用SelectFeature控制的"boxselectionend"事件(需要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 ... 
     } 
    }, 
    {} 
); 
+0

什么是其中只包含一个WMS图层的地图可能的特征?我试过你的代码示例,但“完成”回调函数没有被调用,因为没有选择任何功能,我想......谢谢。 – 2013-03-19 09:47:58

+0

其实我采取Handler.Box的方法,并遇到[这个例子](http://openlayers.org/dev/examples/custom-control.html)。我创建了一个自定义控件,并在那里使用Handler.Box,如示例中所示。 +1并且接受指向正确方向的答案。 – 2013-03-19 09:57:07