- getCurrentPosition(successCallback, errorCallback)
- gets the user current location once
- watchPosition(successCallback, errorCallback)
- regularly keeps polling the user location
function getLocation(){ if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(success, error); console.log('got position'); } else { error('not supported'); } } function error(text){ text = text || 'failed'; console.log(text); } function success(location){ var lat = location.coords.latitude; var long = location.coords.longitude; var url = "http://maps.google.com/maps?q="+ lat+","+long; }
function error (err) { switch(err.code) { case 1: alert("No permission to use geolocation"); break; case 2: alert("Sorry, but a problem happened while getting your location"); break; case 3: alert("Sorry, this is taking too long..."); break; } }
PERMISSION_DENIED = 1 POSITION_UNAVAILABLE = 2 TIMEOUT = 3 code message
Browse into your labs folder in the geolocation folder and edit index.html. This page contains a <div id="map_canvas"> (even though it is not a canvas) that will contain our google map and it also contains three empty JavaScript functions: The first one we call when the page has finished to load is getLocation(). Then the two other functions will be callback function when getting user location: successCallback(pos) and errorCallback(err)