简体   繁体   English

Openlayers 3 forEachFeatureAtPixel对于Points不能正常工作

[英]Openlayers 3 forEachFeatureAtPixel doesn't work correctly for Points

I need to implement "click on feature" functionality using OpenLayers 3. It works fine for all types of features, but when using "Point", forEachFeatureAtPixel fails when I click far enough from centre (close enough to border). 我需要使用OpenLayers 3实现“单击要素”功能。它对所有类型的要素都适用,但是当使用“点”时,当我单击距中心足够远(足够靠近边框)时,forEachFeatureAtPixel会失败。 I have created an example here - https://jsfiddle.net/mstrop/4gvLhfje/7/ . 我在这里创建了一个示例-https: //jsfiddle.net/mstrop/4gvLhfje/7/

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

var found = false;

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

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

When you start clicking on border and you will continue towards the centre of the circle you will see, that the circle is found quite far from the border. 当您开始单击边框时,您将继续朝圆圈的中心前进,您会发现该圆圈离边框很远。 Please, could anybody tell me, what I am doing wrong? 拜托,有人可以告诉我,我做错了吗?

Change 更改

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

to

var pixel = evt.pixel;   

You are looking for this: 您正在寻找这个:

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

The issue with Point geometry is "hidden" in it's property. 点几何的问题在其属性中被“隐藏”。 It uses renderBuffer, which is 100px by default. 它使用renderBuffer,默认情况下为100px。 In case radius of the geometry is bigger, the property has to be increased manually. 如果几何半径较大,则必须手动增加属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM