打开第3层点击图层最佳实践

时间:2016-05-21 08:16:17

标签: openlayers-3

我是OL的新手。

如果我按下特定图层中的特定项目,我需要实现一个应该发生的逻辑。

我必须声明我不是从头开始编写项目,而且我实际上继承了一个非常基本但复杂的系统。

系统从MapGuide 2.5获取图层。

这是地图的启动方式:

 var map = new ol.Map({
            loadTilesWhileInteracting:true,
            layers: this.layers,
            target: this._element[0],
            controls: controls,
            interactions: interactions,
            view: view
        });

 view.fit(that.extent, map.getSize());

我尝试添加一个精选互动 - 它没有用(我的承诺从未被调用过)。

var select_interaction = new ol.interaction.Select();

select_interaction.getFeatures().on("add", function (e) { 
     var feature = e.element; //the feature selected
});

map.addInteraction(select_interaction);

我试过了:

map.on('click', function (evt) {
            var feature = map.forEachFeatureAtPixel(evt.pixel,
              function (feature, layer) {
                  debugger;
                  this.log("fff")
              });
        });

在这种情况下,承诺有效,但我没有任何功能。

修改

我也尝试过:

var feature = map.forEachLayerAtPixel(evt.pixel,
              function (feature, layer) {..}

但我得到例外:

uncaught security error:  Failed to execute 'getImageData' on 'CanvasRenderingContext2D': The canvas has been tainted by cross-origin data.  ol.js:341

我该怎么做?

谢谢, IDO

1 个答案:

答案 0 :(得分:0)

当我尝试通过forEachLayerAtPixel方法在像素处循环图层时,我遇到了同样的错误。

感谢@kagelos评论,我设法通过过滤图层并仅循环遍历 Vector 图层来解决问题。

  

图层类型在OL中大致分为两类。图像图层(图块等)和矢量图层。您必须检查作为地图构造函数中的参数传递的this.layers的内容,并查看它包含的图层类型。您只能直接与矢量类型图层进行交互。

以下是最终代码:

Map.forEachLayerAtPixel( cursorPosition, function ( _layer ) {

  // you will get the vector layer here

}, this, function ( _layer ) {

    if ( _layer instanceof ol.layer.Vector ) {
        return true;
    }
});
相关问题