OpenLayers 3具有用于图层的FeatureAtPixel过滤器 [英] OpenLayers 3 hasFeatureAtPixel filter for layer

查看:1090
本文介绍了OpenLayers 3具有用于图层的FeatureAtPixel过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用以下来自官方OL3示例页面的方法来创建鼠标悬停事件:

I am attempting to create a mouse hover event using the following method taken from the official OL3 examples page:

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

仅在将鼠标悬停在特定图层上时才需要触发操作.查阅了官方文档后,我发现您可以将图层过滤器功能与hasFeatureAtPixel一起使用,但似乎无法正常工作.

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.

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);       
    });
});

console.log调用导致在控制台中提供要素对象,但没有图层对象,这些对象将以未定义"的形式返回.我需要测试图层对象是否正确.

The console.log calls result in feature objects being given in the console, but no layer objects, these are returned as 'undefined'. It is the layer objects which I need to test whether the layer is the correct one.

有什么想法为什么不起作用?

Any ideas why this isn't working?

推荐答案

filter函数将接收一个参数,layer-candidate和 它应该返回一个布尔值.

The filter function will receive one argument, the layer-candidate and it should return a boolean value.

来自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' : '';
});

这篇关于OpenLayers 3具有用于图层的FeatureAtPixel过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆