简体   繁体   English

OpenLayers 3具有用于图层的FeatureAtPixel过滤器

[英]OpenLayers 3 hasFeatureAtPixel filter for layer

I am attempting to create a mouse hover event using the following method taken from the official OL3 examples page: 我正在尝试使用从官方OL3示例页面采用的以下方法创建鼠标悬停事件:

http://openlayers.org/en/latest/examples/earthquake-clusters.html http://openlayers.org/en/latest/examples/earthquake-clusters.html

I need to trigger the action only when hovering over a particular layer. 仅在将鼠标悬停在特定图层上时才需要触发操作。 Having consulted the official documentation I discovered that you can use a layer filter function with hasFeatureAtPixel, but it doesn't appear to be working. 在查阅了官方文档后,我发现可以将图层过滤器功能与hasFeatureAtPixel一起使用,但是它似乎不起作用。

map.on('pointermove', function(evt) {
    if (evt.dragging) {
       return;
    }
    var pixel = map.getEventPixel(evt.originalEvent);
    var hit = map.hasFeatureAtPixel(pixel, function(feature, layer) {
        console.log(layer);
        console.log(feature);       
    });
});

The console.log calls result in feature objects being given in the console, but no layer objects, these are returned as 'undefined'. console.log调用导致在控制台中提供要素对象,但没有图层对象,这些对象将作为“未定义”返回。 It is the layer objects which I need to test whether the layer is the correct one. 我需要测试图层对象是否是正确的图层对象。

Any ideas why this isn't working? 任何想法为什么这不起作用?

Actually the API is rewritten (v4.0.1), the working example is as follows: 实际上,API已被重写(v4.0.1),工作示例如下:

var hit = map.hasFeatureAtPixel(e.pixel, {
    layerFilter: function (layer) {
        return layer.get('name') === 'test';
    }
});

The filter function will receive one argument, the layer-candidate and it should return a boolean value. 过滤器函数将接收一个参数(layer-candidate),并且应返回一个布尔值。

From API Docs. 来自API文档。

Let's say you have a layer like: 假设您有一个类似的图层:

var vectorLayer = new ol.layer.Vector({
  name: 'test',
  // ...
});

You can add a layer filter function like: 您可以添加一个图层过滤器功能,例如:

map.on('pointermove', function(e) {
  if (e.dragging) return;

  var hit = map.hasFeatureAtPixel(e.pixel, function(layer) {
    return layer.get('name') === 'test'; // boolean
  });
  map.getTarget().style.cursor = hit ? 'pointer' : '';
});

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

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