Now, we are ready to test our new setup with a fully mobile-capable OpenLayers application. As you'll see in the following steps, there's nothing special you need to do with OpenLayers itself to work in a mobile environment:
mobile.html:<!doctype html>
<html>
<head>
<title>Mobile Example</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="../assets/ol3/ol.css" type="text/css" />
<link rel="stylesheet" href="../assets/css/samples.css" type="text/css" />
</head>
<body>
<div id="map" class="full-map"></div>
<script src="../assets/ol3/ol.js"></script>
<script>
var layer = new ol.layer.Tile({
source: new ol.source.OSM()
});
var london = ol.proj.transform([-0.12755, 51.507222], 'EPSG:4326', 'EPSG:3857');
var view = new ol.View({
center: london,
zoom: 6,
});
var map = new ol.Map({
target: 'map',
layers: [layer],
view: view
});
</script>
</body>
</html>
We created an example very similar to the ones we've been using in previous chapters and, just by loading it on a mobile device, we get new capabilities from OpenLayers' default touch interactions. We can now pinch to zoom in and out, move two fingers in a circular motion to rotate the map, and touch drag the map to pan around.
We made a couple of small changes to the example we used in previous chapters though, so let's highlight the differences. In the <head> section, we've added a <meta> tag. The <meta> tag provides information to the web browser rendering the page, its name indicates the type of information it is providing, and its content is the information or data we want to inform the browser about. In this case, we've used the viewport <meta> tag to provide some information that mobile web browsers can use to modify how a user interacts with the web page in a mobile browser. Let's break down the content and explain what it's doing, as follows:
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
The content is a series of comma-separated key-value pairs formatted as key=value. The width key indicates how wide the browser's viewport should be. It can be set to a specific number of pixels or to a predefined value device-width, which indicates that the viewport should be 100 percent wide. The initial-scale and maximum-scale keys constrain the mobile browser viewport scale factor, which would allow rendering a page more zoomed-in than its default. Setting the user-scalable key to no disables zooming the web page in response to the user performing a pinch gesture. We want the pinch gesture to zoom the map, not the whole page. We've also changed the class name used on the HTML element that contains the map from map to full-map. This changes the map element to take up 100 percent of the width and the height of the browser viewport.
The remainder of the example is exactly the same as in previous chapters. In fact, you can load this example in your normal web browser, and it should function in exactly the same way as many of the other examples, the only difference being that the map will fill your browser window.
So, we get touch support in our web mapping application without really having to do anything at all. But just supporting touch events isn't that exciting. Let's explore what else OpenLayers has to offer on mobile devices, starting with finding our location.