October 2017
Intermediate to advanced
370 pages
8h 57m
English
Accessing the device location with the HTML5 Geolocation API is straightforward. First check for Geolocation API support with:
if (navigator.geolocation) { // Do something}
Then, if supported, we can retrieve the location with the getCurrentPosition() method like this:
navigator.geolocation.getCurrentPosition(function(location) { // Do something with location data});
This method takes a callback function, which has access to the user's location as location.coords.latitude and location.coords.longitude. In this function we can now do something with the location, such as centering the map on the location, and zooming in a little:
navigator.geolocation.getCurrentPosition(function(location) { var position = {Read now
Unlock full access