Let's create a new OpenLayers application and identify the components as we go:
components.html in the sandbox directory and put in the standard HTML structure for our applications as follows:<!doctype html>
<html>
<head>
<title>OpenLayers Components</title>
<link rel="stylesheet" href="../assets/ol3/css/ol.css" type="text/css" />
<link rel="stylesheet" href="../assets/css/samples.css" type="text/css" />
</head>
<body>
<div id="map" class="map"></div>
<script src="../assets/ol3/js/ol.js"></script>
<script>
</script>
</body>
</html>layers collection:var layer = new ol.layer.Tile({
source: new ol.source.OSM()
});DragRotateAndZoom:var interaction = new ol.interaction.DragRotateAndZoom();
FullScreen:var control = new ol.control.FullScreen();
<div> tag containing our map:<div id="overlay" style="background-color: yellow; width: 20px; height: 20px; border-radius: 10px;">
var center = ol.proj.transform([-1.812, 52.443], 'EPSG:4326', 'EPSG:3857');
var overlay = new ol.Overlay({
position: center,
element: document.getElementById('overlay')
});var view = new ol.View({
center: center,
zoom: 6
});var map = new ol.Map({
target: 'map',
layers: [layer],
interactions: [interaction],
controls: [control],
overlays: [overlay],
view: view
});
If you try to interact with the map, you'll notice something immediately—you can't click and drag to pan. Instead, if you hold down the Shift key while clicking and dragging, you'll get a whole new behavior. We've also lost the zoom in and out buttons at the top left corner, and the attribution icon in the bottom right corner, but there is a new button in the top-right corner. What does it do?
In the first step, we added our boilerplate HTML code for a simple OpenLayers application. This includes standard HTML tags suitable for any web page, and also includes the OpenLayers CSS and JavaScript files.
Next, we started creating instances of various OpenLayers components to use with our map. The first one was a layer that renders tiles from the OpenStreetMap tile servers. Next, we created an interaction called DragRotateAndZoom. This interaction has no visible element, but rather is triggered by holding down the Shift key while dragging the map. As the name suggests, this interaction will rotate and zoom the map in response to dragging the mouse cursor. Then we created a control called FullScreen. Again, the name gives away what it does—launches the web page into fullscreen mode. Because it is a control, it has a button that is clicked to activate its behavior. Next, we created an overlay, which displays an HTML element at a specific geographic location. Finally, we created the view, giving it the same center as the position we provided to the overlay.
The last step is to tie it all together with the Map class object by passing all our components as part of the constructor. We'll look at the Map class's constructor in more detail in Chapter 3, Charting the Map Class but for now, it's enough to know that we can provide arrays of layers, interactions, controls, and overlays as options, and the Map class will know what to do with them.
We saw that by providing our own set of interactions and controls; we changed the default behavior of an OpenLayers application. This is because the Map class has a default set of interactions and controls that it creates when they are not explicitly provided as options to the constructor. There are two ways we can restore the default behavior and add our new components as well.
The first way is to use helper functions provided by OpenLayers to obtain the collection of default interactions and default controls and extend those with our new components. We will do something like this (this example is not complete as the Map class is missing a view):
// create our interaction
var interaction = new ol.interaction.DragRotateAndZoom();
// get the default interactions and add our new one
var interactions = ol.interaction.defaults().extend([interaction]);
// create our control
var control = new ol.control.FullScreen();
// get the default controls and add our new one
var controls = ol.control.defaults().extend([control]);
// create the map and pass in the extended set of interactions and controls
var map = new ol.Map({
interactions: interactions,
controls: controls
});The second method is to add them to the map after calling the constructor, for instance, (again, this example is not complete):
var interaction = new ol.interaction.DragRotateAndZoom(); var control = new ol.control.FullScreen(); var map = new ol.Map(); map.addInteraction(interaction); map.addControl(control);
The advantage of the first method (passing constructor options) is that it gives you complete control over which interactions and controls are in use, while the second method (adding them after creating the map) has the advantage of being simpler if you want to add to the existing set of interactions and controls.
In many parts of OpenLayers, you will discover that there are multiple ways of accomplishing a given task. This is part of the design of the OpenLayers library and provides developers with the option to write code in a way that makes most sense for them.