最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - OpenLayers 3 hasFeatureAtPixel filter for layer - Stack Overflow

programmeradmin0浏览0评论

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

.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.

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'. It is the layer objects which I need to test whether the layer is the correct one.

Any ideas why this isn't working?

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

http://openlayers/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.

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'. It is the layer objects which I need to test whether the layer is the correct one.

Any ideas why this isn't working?

Share Improve this question edited Sep 6, 2016 at 21:34 Single Entity asked Sep 6, 2016 at 17:11 Single EntitySingle Entity 3,1233 gold badges42 silver badges66 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Actually the API is rewritten (v4.0.1), the working example is as follows:

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.

From API Docs.

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' : '';
});
发布评论

评论列表(0)

  1. 暂无评论