Saturday, 4 August 2018

Using OpenLayers 3 forEachFeatureAtPixel to specify layer?


I must first state that my skills at javascript are about the same as my skills in Spanish. I know barely enough to get myself into trouble and then I go looking for help. anyway I have an application that has a few geoJSON layers in it. I have created a map.forEachFeatureAtPixel function to update a div with some information.


The problem is it looks at all of the layers and I just want it to look at one. this way I can create another function to get some different attributes from another layer. I have looked over the documentation and I see the opt_layerFilter option but I dont know how to apply it (due to my lack of Javascript knowledge). If someone could give me a hint here hopefully I will then be able to read the documentation and stop asking these questions.


function:


var displayFeatureInfo = function(pixel ) {
var feature = map.forEachFeatureAtPixel(pixel, function(feature, layer) {
return feature;
});


var info = document.getElementById('info');
if (feature) {
info.innerHTML = '

' + feature.get('Name') +
'

' + '
API:
' + feature.get('API') + '
'
+ '
Status:
' + feature.get('Status') + '
' +
'
Legal :
' + feature.get('Legal') + '
' +
'
Site Name:
' + feature.get('Site_Name') + '
'+'
';
} else {
info.innerHTML = ' ';

}

if (feature !== highlight) {
if (highlight) {
featureOverlay.removeFeature(highlight);
}
if (feature) {
featureOverlay.addFeature(feature);
}
highlight = feature;

}

};

Answer



You can use the opt_layerFilter argument to forEachFeatureAtPixel. See the documentation at http://openlayers.org/en/master/apidoc/ol.Map.html#forEachFeatureAtPixel.


For example, if vectorLayer is a reference to the layer in which you want to detect features you will use the following:


var feature = map.forEachFeatureAtPixel(pixel, function(feature, layer) {
return feature;
}, null, function(layer) {
return layer === vectorLayer;

});

With this "filter function" layers that are not vectorLayer will be filtered out.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...