2016-07-25 53 views
3

我試圖激活一個交互時,鼠標懸停在某些功能。Openlayers 3 - Interaction和pointermove

它正在如此工作......問題在於,如果您慢慢移動鼠標,交互會保持活動狀態。

這是OL3上的錯誤,還是我應該以不同的方式做?

代碼:http://jsfiddle.net/gmaq54dm/3/

olMap.on("pointermove", function (e) { 
    if (e.dragging) { 
     return; 
    } 
    var map = e.map; 
    console.log(e.pixel); 
    var feature = map.forEachFeatureAtPixel(e.pixel, function(feature, layer) { 
     return feature; 
    }); 
    var hit = (feature) ? true : false; 
    console.log(hit); 
    olDraw.setActive(hit); 
}); 

感謝

+0

它看起來像一個錯誤。你可以向核心開發者報告。 –

+0

我會在'setTimeout'調用中包裝最後一行,以便在處理完pointent事件後調用:'window.setTimeout(function(){olDraw.setActive(hit);},0);' – ahocevar

回答

2

這是你的應用程序中的錯誤,而不是在的OpenLayers。您需要確保您只能從矢量圖層中檢測特徵,而不是從繪圖層中檢測特徵。更改forEachFeatureAtPixel功能

var feature = map.forEachFeatureAtPixel(e.pixel, function(feature, layer) { 
    return feature; 
}, null, function(layer) { 
    return layer == vectorLayer 
}); 

最後一個參數添加一個圖層過濾器,只打了檢測矢量層上的特徵。

更新,工作JSFiddle:http://jsfiddle.net/gmaq54dm/4/

+0

謝謝安德烈亞斯!它完美的作品! –

0

我@jonatas同意,這似乎是一個錯誤。

雖然,有一個解決方法,似乎做你的工作。

  1. 避免多行字符串。它讓ol3更加複雜,以驗證鼠標何時結束該功能。
  2. 使用vectorSource.forEachFeatureInExtent(,然後使用鼠標座標創建一個小矩形,並在每個方向上添加幾米。這將確保您的鼠標「mbr」落入特徵中。 請注意,我使用鼠標座標+ -5米來構建mbr。您必須調整以滿足您的要求。

檢查fiddle here

+0

好! !我會嘗試這種方式。謝謝 –