When a vector layer is populated with features, it can be very useful to query the data source in order to retrieve feature information. OpenLayers provides some methods from the vector source class (ol.source.Vector) that enable us to perform queries, such as finding out what features are within a custom extent (getFeaturesInExtent), or returning any features at a particular coordinate (getFeaturesAtCoordinate), as well as other useful types of queries.
We are going to create a map with a tile raster layer and a vector layer with features from a custom GeoJSON file. The polygon features will represent campsites within a region, each with some properties that can be extracted for display. We will call the getFeaturesAtCoordinate source query method when the map is clicked on and display the applicable feature information within overlay if a feature exists at this coordinate.
The source code can be found in ch05/ch05-feature-info-from-source, and here's a screenshot of what we'll end up with:

In order to find out how to retrieve feature information from a data source, follow these steps:
div to contain the map. In particular, add the markup for overlay and content:<div id="js-overlay" class="overlay"> <ul> <li><strong id="js-ref"></strong></li> <li><span id="js-restrictions"></span></li> </ul> </div>
var vectorSource = new ol.source.Vector({
url: 'geojson.json',
format: new ol.format.GeoJSON({
defaultDataProjection: 'EPSG:3857'
})
});map with a raster tile layer, vector layer, and view: var map = new ol.Map({
view: new ol.View({
zoom: 15, center: [872800, 6065125]
}),
target: 'js-map',
layers: [
new ol.layer.Tile({source: new ol.source.OSM()}),
new ol.layer.Vector({source: vectorSource})
]
});var overlayElem = document.getElementById('js-overlay');
var featureRefElem = document.getElementById('js-ref');
var featureRestrictionsElem = document.getElementById(
'js-restrictions'
);overlay, add it to map, and set the CSS display:var overlay = new ol.Overlay({
element: overlayElem
});
map.addOverlay(overlay);
overlayElem.style.display = 'block';singleclick map event and display the feature information within overlay if appropriate:map.on('singleclick', function(event) {
overlay.setPosition(undefined);
var features =
vectorSource.getFeaturesAtCoordinate(event.coordinate);
if (features.length > 0) {
overlay.setPosition(event.coordinate);
featureRefElem.innerHTML = features[0].get('ref');
featureRestrictionsElem.innerHTML =
features[0].get('restrictions');
}
});We have excluded much of the HTML and all CSS from this recipe for brevity, but please take a look at the source code for the complete implementation.
Much of this recipe will be similar to earlier examples in this book, so let's narrow in on the map click event handler and the source query that we utilize:
map.on('singleclick', function(event) {
overlay.setPosition(undefined);
var features =
vectorSource.getFeaturesAtCoordinate(event.coordinate);We subscribe to the singleclick event published by the map object when a user clicks or taps on the map. The overlay method is passed an argument of undefined so that it's hidden until we know that we wish to display it.
We make use of the getFeaturesAtCoordinate source query method to determine whether or not a feature exists at the coordinate of the click event (event.coordinate). The result is stored in a variable, namely features.
if (features.length > 0) {
overlay.setPosition(event.coordinate);
featureRefElem.innerHTML = features[0].get('ref');
featureRestrictionsElem.innerHTML =
features[0].get('restrictions');
}Although we know that our data contains no overlapping features at any given coordinate, with other data sources this scenario may occur, which is why OpenLayers returns the result of the query as an array of features. We check whether the length of the array is greater than zero; if this is the case, we can infer that a feature is present at this particular coordinate.
The overlay is positioned at the coordinate via the setPosition method, and the overlay content is populated with the feature ref property and the restrictions property, both via the get method that ol.Feature inherits from ol.Object. The text is added to the relevant DOM elements using the JavaScript innerHTML method.
We can even use the getFeaturesAtCoordinate source query method with a coordinate, where a feature resides but is not currently visible from the viewport extent. It will still return the feature detected at this location.
Other powerful data source queries exist, such as getClosestFeatureToCoordinate and getFeaturesInExtent, which could be very useful when developing mapping applications. I recommend that you visit the OpenLayers documentation (http://openlayers.org) to discover other useful methods and find out what these methods can do for you.