Let's try with another example to discover more about the forEachFeatureAtPixel method. It should remind you about the last sample from Chapter 5, Using Vector Layers. To complete the task, check the following steps:
2360_08_01_simple_select.html in a new HTML file 2360_08_03_foreachfeatureatpixel.html.ol.js JavaScript file reference with ol-debug.js.map.on('click' , function(evt) {
var pixel = evt.pixel;
console.log(evt);
console.log(pixel);});
console.log statements, and add the following code just after the line var pixel = evt.pixel;displayFeatureInfo(pixel);.displayFeatureInfo before the map.on( code:var displayFeatureInfo = function(pixel) {
var features = [];
map.forEachFeatureAtPixel(pixel, function(feature, layer) {
features.push(feature);
});
console.log(features);};
console.log with this code:var container = document.getElementById('information');
if (features.length > 0) {
var info = [];
for (var i = 0, ii = features.length; i < ii; ++i) {
info.push(features[i].get('N3NM'));
}
container.innerHTML = info.join(', ') || '(unknown)';
} else {
container.innerHTML = ' ';
}<div id="information"></div>

First, with console.log, we tried to see the event content and also get the pixel position with an array of x, y, where we click. Next, we made a call to the function that relies on a map.forEachFeatureAtPixel function. Its purpose is to get back every feature under the click using the pixel position from the event where we want to see the return you get from the callback in success property and also restrict the action on the vectorEuropa layer.
We also saw that the return contained the ol.Feature object, and because of this, we chose to play to try if the return was empty or not. As a click can send back more than one feature, we used a loop to push the features property N3NM information in an array. Depending on this, we have the choice to display or not the name of the NUTS region with the attribute N3NM in a DOM element in the screenshot FR717 Savoie.
After this review, we know how to retrieve and manipulate information from vector layers. You understand that the method map.forEachFeatureAtPixel within displayFeatureInfo sends back ol.Feature. You can manipulate to retrieve geometry or attributes from the layers you click on.
There is another way to retrieve information when clicking, and it's with the WMS getGetFeatureInfoUrl method. In which circumstances should we use it, and why does it exist?
The title can be unclear if you don't already have some basics.
For this, we will start by reviewing the Open Geospatial Consortium (OGC) Web Map Services (WMS) standard.
Let's begin with the basics of WMS, and later, we will come back to the main topic.
For this, we will reuse two excerpts from the Geoserver documentation (a server-side component that can work with the OpenLayers 3 library).
The first one summarizes what WMS is:
"The OGC Web Map Service (WMS) specification defines an HTTP interface for requesting georeferenced map images from a server."
The second one, a table, gives you the type of requests the WMS standard can perform:
In this list of possible operations, the most common operation is GetMap, an operation that send back an image to display in a client, JavaScript or desktop.
But you also see GetFeatureInfo, an optional operation for WMS.
Here, when you read, the point is that it's not the client side that manages returned features, but the server side. The purpose here is to have less load on the client side: managing a lot of features in the client side is mainly a pain, in particular if you use DOM.
The idea is to send the information to a server and retrieve the minimum and really light result. The result can be an HTML, a GML, raw text or depending on your server, also a GeoJSON. To illustrate, type into your browser the following URL:
You will see an HTML table with information from the point where we clicked on: INFO_FORMAT= text/html. It is the way to say we want an HTML format return.
As a conclusion, just retain at the moment that when you choose to use the OpenLayers getGetFeatureInfoUrl method, you are supposed to use it with WMS layers.
Now, you have enough knowledge, let's go back to the main topic.