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:
First, copy and paste the code from Time for action – converting your local or national authorities data into web mapping formats, that is, the file
2360_08_01_simple_select.html
in a new HTML file2360_08_03_foreachfeatureatpixel.html
.Then change the
ol.js
JavaScript file reference withol-debug.js
.After that, add at the end of the JavaScript section of the new HTML file the following:
map.on('click' , function(evt) { var pixel = evt.pixel; console.log(evt); console.log(pixel);});
Open your browser with the console, and click where you have blue features to get a result like the following screenshot:
Next, remove both
console.log
statements, and add the following code just after the linevar pixel = evt.pixel...