The arrival of Google Maps led to an explosion in the world of GIS and web mapping. Google introduced not only an API but also certain file formats.
The Keyhole Markup Language (KML) has become one of the most extensively-used formats, and it has become an OGC standard. For some more details on the KML standard, refer to https://en.wikipedia.org/wiki/Keyhole_Markup_Language. The following screenshot shows how to create vector layers from a KML file:

This recipe (found in ch03/ch03-kml-layer/) will show you how easy it is to add features from a KML file. You can find the necessary files in the KML format attached to the source code of this book and available on the Packt Publishing website.
In order to import features from KML format into the map, follow these instructions:
div to contain the map:<div id="js-map"></div>
var map = new ol.Map({
view: new ol.View({
zoom: 16,
center: [3465642, 3500474]
}),
target: 'js-map',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM(),
opacity: 0.4
}),
new ol.layer.Vector({
source: new ol.source.Vector({
url: 'egypt-pyramids.kml',
format: new ol.format.KML()
})
})
]
});The instantiation of the vector layer follows the same structure as seen in the previous GML layer recipe. The only noticeable change is the difference in url and format:
new ol.layer.Vector({
source: new ol.source.Vector({
url: 'egypt-pyramids.kml',
format: new ol.format.KML()
})
})OpenLayers makes an AJAX request for the KML file from the string passed to the url property. The format (ol.format.KML) corresponds to the KML file request so that OpenLayers knows how to process the response. The ol.format.KML class provides some properties that can be customized on instantiation. One of which is extractStyles. This property informs OpenLayers whether or not to extract feature styling information from the KML file (such as color and line width) and use it to render on the map. This defaults to true.
Like GML, the KML format offers tons of options and possibilities at the cost of complexity. Google has some useful documentation to help developers structure their KML files at https://developers.google.com/kml/documentation
In the KML format, placemarks can have a description attached to them, and if you load a KML file in Google Maps, the placemark's description is shown as a balloon (or a popup) when you click on it. With OpenLayers, this kind of behavior requires some manual intervention to achieve similar results.