I'm trying to teach myself OpenLayers and took an introductory course in which we received the whole code. For my first own project I want to do something similar and recycle some of the code. But there is some part of the code I don't understand and hope that someone can explain it to me. The idea is that the user can click on the vector element on the map (schools in this case) and recive information about its attributes. Here is the code:
map.on('singleclick', function(e) {
var feature = map.forEachFeatureAtPixel(e.pixel, function(feature) {
return feature;
});
var schoolName = feature ? feature.get('name') : '';
alert(schoolName);
});
All right, when the user clicks on the map, it fires the function e. In this function we create a variable with the name feature and this variable recieves the function forEachFeatureAtPixel, which returns the variable feature. Thus, the way I understand it the value of the variable feature is itself. Thus, it shouldn't have a value. Where is my logic mistake?
Furthermore I don't understand the next line either. We create a variable schoolName and this variable gets the value "feature ? feature.get('name') : ' ';
But what does the "?" do and why do we need : ' ';
Can someone explain these lines of code?