To illustrate the simplest use case for ol.Overlay, let's perform the following steps:
2360_08_01_simple_select.html, we always used in the chapter, in a new file 2360_08_05_simple_overlay.html. You can also check the code at the Packt code book URL.assets/css/samples.css this code:#popup {
background: red;
}<div id="map" class="map"></div> with <div id="map" class="map">, as follows:<div id="popup"><b>OpenLayers 3 Code Sprint</b> <i>Humanities A3</i></div> </div>
var popup = new ol.Overlay({
element: document.getElementById('popup')
});
var osmLayer = new ol.layer.Tile({
source: new ol.source.OSM()
});
var ol3_sprint_location = ol.proj.transform([-1.20472, 52.93646], 'EPSG:4326', 'EPSG:3857');
var view = new ol.View({
center: ol3_sprint_location,
zoom: 16
});
var map = new ol.Map({
target: 'map'
});
map.addLayer(osmLayer);
map.setView(view);
map.addOverlay(popup);
popup.setPosition(ol3_sprint_location);
Let's review the code to understand the way ol.Overlay works.
First, we defined a new ol.Overlay with the following:
var popup = new ol.Overlay({
element: document.getElementById('popup')
});The element property in the object literal option of the constructor has to reference an HTML element.
Here, we reference the HTML text <b>OpenLayers 3 Code Sprint</b> <i>Humanities A3</i> located in a <div> tag with an id value of popup.
Next, we performed the usual steps, such as creating layers, creating map, adding the layers, and setting the view, as follows:
var osmLayer = new ol.layer.Tile({
...
...
map.setView(view);Just after this block, we added the ol.Overlay object to the map in order to declare that we are using an overlay:
map.addOverlay(popup);
Instead of adding overlays after the preceding map, you can also use the option in the ol.Map constructor, as shown in the following sample:
var map = new ol.Map({
target: 'map',
overlays: [popup]
});The following line was to set the position of the DOM element by matching coordinates and image position:
popup.setPosition(ol3_sprint_location);
The name overlay implies also that there is something at the top of something else. To understand, just see the screenshot of the Chrome Developers Tools Elements panel, as follows:

As you see, the pop-up is included in a div tag, also contained in a div tag with class ol-overlaycontainer situated after the canvas element (where the map is drawn).
Now, let's assemble our knowledge with overlay and map feature methods.