Let's add another resource to our page, so that we can access some nice icons. In the past, we might have done this by creating images for each of the icons we wanted and then adding <img> tags. We are going to try a different approach this time using an iconic font called Font Awesome. We'll discuss why after the example; for now, just follow these steps:
<link> tag to the <head> tag of your HTML document, just after the link to ol.css:<link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet">
<style> section inside the <head> tag:<style>
.marker {
position: absolute;
width: 24px;
height: 24px;
font-size: 24px;
}
</style><div> tag just after the map <div> tag:<div class="map"></div> <div id="location" class="marker icon-flag"></div>
<script> tag, after all the other code:var marker = new ol.Overlay({
element: document.getElementById('location')
});
map.addOverlay(marker);
var geolocation = new ol.Geolocation({
tracking: true
});
geolocation.bindTo('projection', view);
geolocation.bindTo('position', marker);Step 1 adds a link to a stylesheet that defines an iconic font, which we'll discuss in more detail in just a moment. In step 2, we created a new CSS class called marker that we can add to the HTML elements we want to be displayed as markers on the map. This class sets the width and height of the element to be 24 pixels and also the font size, as we are using an iconic font for our marker images. In step 3, we added the HTML element we want to use as our marker. We gave it an ID of location, so that we could refer to it later and added both marker and icon-flag to the class. The marker class makes the element a specific size, and the icon-flag class indicates which icon to use from our iconic font. In step 4, the new code we added creates a new ol.Overlay attached to our marker element and adds it to the map. Then, we create an instance of ol.Geolocation and tell it we want to track location updates. Then, we bind the projection property of the geolocation object to the view's projection and position properties to the marker. The geolocation object will now automatically convert location updates into the correct projection for our view and move our marker overlay to our current location.
Iconic fonts are just like any other font, except that the characters in the font are designed as pictures instead of letters. Like text in a web page, the icons in a font are easily resizable and adapt to different resolution displays automatically. Mobile devices such as the iPhone 4 and later sport retina displays have a high pixel density. Normal images on retina devices end up looking blocky. Icon fonts are automatically rendered at a higher resolution on retina devices and are crisp and clear.
FontAwesome (http://fortawesome.github.io/Font-Awesome/) is a popular iconic font (at the time of writing this) and is easy to use in any web application, just by including a single <link> tag and then using icon-specific class names. We'll use a couple more Font Awesome icons in later examples, but if you are interested in what icons are available, visit their site and take a look around.
Font Awesome isn't the only choice for an icon font, and there are many tools on the Web to help you make your own icon fonts from existing vector art.
Now that we've seen ol.Geolocation in action, let's take a look at its full capabilities.
When creating an instance of ol.Geolocation, you can provide the following optional properties to control its initial behavior:
The Geolocation class inherits from ol.Object and has the following key-value observing (KVO) properties (recall that we discussed KVO in Chapter 2, Key Concepts in OpenLayers):