2016-03-11 278 views
0

我需要使用OpenLayers 3實現「點擊功能」功能。它適用於所有類型的功能,但使用「點」時,forEachFeatureAtPixel失敗,當我點擊足夠遠從中心(足夠接近邊界)。我在這裏創建了一個示例 - https://jsfiddle.net/mstrop/4gvLhfje/7/Openlayers 3 forEachFeatureAtPixel無法正常工作點

var pixel = map.getEventPixel(evt.originalEvent); 

var found = false; 

map.forEachFeatureAtPixel(pixel, function(feature, layer) 
{ 
    found = true; 
}); 

console.log((found?"":"not ") + "found"); 

當你開始點擊邊框,將繼續努力,你會看到在圓心,這個法陣是從邊境很遠發現。請有人告訴我,我做錯了什麼?

回答

0

與點幾何的問題是 「隱藏」 在它的屬性。它使用renderBuffer,默認爲100px。如果幾何體的半徑較大,則必須手動增加屬性。

0

變化

var pixel = map.getEventPixel(evt.originalEvent); 

var pixel = evt.pixel; 
+0

謝謝,但沒有什麼區別 - https://jsfiddle.net/mstrop/4gvLhfje/8/ – user3523426

0

您正在尋找這樣的:

map.on('click', function(evt) { 
    var feature = map.forEachFeatureAtPixel(evt.pixel, function(ft){return ft;}); 
    if (feature) { 
    // ... 
    } 
});