Generating geocaches
In the next example, called ch08_points
, we create some random geocaches based on the user's location. As server-side scripting and client-server connections are also out of the scope of this book, we make our application viable by adding random points within 500 meters of our position. Firstly, we create an empty layer for the points and initialize Geolocation:
var geoloc = new ol.Geolocation({ projection: map.getView().getProjection(), tracking: true }); var geoCaching = new ol.layer.Vector({ source: new ol.source.Vector() }); map.addLayer(geoCaching);
The ol.Geolocation
constructor creates a simple wrapper object around the HTML5 Geolocation API. It is capable of everything the Geolocation API can do, and provides convenience methods to register listeners on changes and access Geolocation-related information. As we need our position to generate local points, we must register a listener to the Geolocation
object's change:position
event. As we do not want...