The map object is the central component of an OpenLayers web application. It is a central place to add and remove things such as layers and controls, and bind them all together. The remaining chapters in the book will introduce you to these other things, but one of those bits is really very closely tied to the map object, and that is the view object. The view object provides the map with the information it needs to decide what location and level of detail—or zoom level—you are looking at. A view also has a projection (which we discussed in Chapter 2, Key Concepts in OpenLayers) that determines the geospatial reference system of the map.
OpenLayers currently provides a single View class, ol.View. This class represents a simple 2D view, which can be manipulated through three key properties: center, resolution, and rotation. We will create a new instance of ol.View in the same way that we create a map object, like the following:
var view = new ol.View(ViewOptions);
We've used views in all our examples because a view is a mandatory parameter when creating a new map instance, but we haven't discussed it in detail yet. Let's dive in.
The options you can use when creating a new ol.View instance are as follows:
It is important to understand what is meant by the term resolution and how the various view options related to resolution work together.
In the context of OpenLayers, the term resolution means the number of projection units per pixel. A projection determines how the real world, which is a sphere, is represented in 2D space. The definition of a projection includes a number of things, including a 2D coordinate system and an algorithm for converting real-world locations (latitude and longitude) to and from the projection's coordinate system. The projection's coordinate system is defined in a particular set of units, such as meters, feet, or even decimal degrees. The projection's coordinate system has a bounding box defined as values in the projection's unit system. When we say projection units per pixel, we mean a value expressed in the unit system of the projection.
The most zoomed out state of the map will render the bounds of the projection into a 256 x 256 pixel tile. This is considered the maximum resolution and can be calculated as the width of the projection's bounds divided by 256. When the map is at its most zoomed in, the minimum resolution can be calculated as the width of the projection's bounds divided by the number of tiles wide times 256. We yet don't know how many tiles are required to draw the map at the most zoomed in level; so, this has to be known or computed in some way.
The most zoomed out state of the map is, by convention, called ZoomLevel 0. When a user zooms in or out, they change to the next zoom level. Zooming effectively changes the number of tiles used to represent the projection's bounds. The default zoom factor is 2, which means that zooming in to the next zoom level doubles the number of tiles in the map (in each dimension) and halves the resolution. If ZoomLevel 0 is 1 tile (wide and high), then ZoomLevel 1 is 4 tiles (2 wide and 2 high). ZoomLevel 3 is 16 tiles (4 wide and 4 high). In fact, the number of tiles required for the width and height of any zoom level can be easily calculated as numTiles = 2 ^ zoom. Each zoom level has a resolution defined by the projection's bounds divided by the zoom level's width in tiles. For any given zoom level z, the resolution can be computed as resolution = projection width / (2 ^ z). Likewise, the zoom level equivalent to any resolution can be computed by rearranging the algorithm to z = log(projection width / resolution)/log(2).
When a view is configured, it needs to know a maximum and minimum resolution and a zoom factor for determining the resolutions between the maximum and minimum for zooming. The View class provides several options that are used together to determine the resolution constraints. Since zoom level and resolution can be computed from each other, the constraints can be determined in terms of resolution (maxResolution and minResolution), zoom levels (minZoom is equivalent to maxResolution, maxZoom is equivalent to minResolution), or some combination of these. OpenLayers uses the resolution options in preference to the zoom options if both are provided.
In practice, it is usually sufficient to define just the maxZoom or minResolution because minZoom defaults to 0 (the maximum resolution computed from the projection bounds).
As with the Map class, the View class has a number of KVO properties that have accessor methods (get and set), property binding, and events as discussed earlier:
The View class has several methods in addition to its properties: