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 badges2 Answers
Reset to default 5Actually 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' : '';
});