Customizing the view is not a hard task; it manly consists of considerations regarding the nature of the project and expected outcome. For our application, wrapping the map around the x axis is completely needless. Also, we can restrict the extent to the projection's extent. In the first example, called ch06_customize, we make such modifications in our application. First, we disable the layer wrapping in our base layers:
var map = new ol.Map({
[…]
new ol.layer.Tile({
source: new ol.source.TileWMS({
[…]
wrapX: false
[…]
new ol.layer.Vector({
source: new ol.source.Vector({
[…]
wrapX: false
}),With this consideration, we gained an instant boost in performance. However, we have to say goodbye to it instantly as this will make our map more responsive. By default, when we are interacting with the map or an animation takes place, tile and vector layers wait for us to finish before they get updated. We can disable this phenomenon with two options. In case of vector layers, we have to disable it in the layer level, but for tile layers, we can disable it in the
map object. We can do it in this way:
new ol.layer.Vector({
[…]
updateWhileAnimating: true,
updateWhileInteracting: true
})
[…]
loadTilesWhileAnimating: true,
loadTilesWhileInteracting: true
});In the next step, we'll modify the view object to restrict the maximum extent. Note that we can pan outside the map's extent since this option only constrains the center of the map:
view: new ol.View({
[…]
extent: ol.proj.get('EPSG:3857').getExtent()
}),Finally, we enable some keyboard interactions. There are two interactions related to the keyboard events in OpenLayers 3, and both of them get added to the map by default. However, we cannot use them as only focused elements can receive keyboard events, and only input-related elements can be focused by default. To make our map's div element focusable, we must add a tabindex property to it in the HTML file:
[…] <div id="map" class="map" tabindex="-1"></div> […]
Now, if we click on the map, we can pan with the arrow keys and zoom in with the + (plus) and - (minus) keys.
You can provide -1, 0, or any positive integer to the tabindex property. The -1 value opts out the element from focusing with the Tab key, but it makes it focusable with a mouse click or with the focus method. The 0 value does not alter the natural focusing order with the Tab key, while a positive integer determines its order.
If a focusable element gains focus, some browsers render an outline on the border of the element. As we do not want to have an outline on our map, we will disable it in our CSS file by extending our map class with a single rule:
.map {
[…]
outline: 0;
}