-
-
Save erichurst/334504 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Geo = navigator.geolocation, // {Object|null} a shortcut to the geolocation static class | |
options = { // {Object} options available to calls for position | |
enableHighAccuracy: false, // {Boolean} Whether or not to use more resources to get a more accurate position | |
maximumAge: 0, // {Number|Infinity} The maximum number of milliseconds since the last check. | |
timeout: null // {Number|null} The maximum number of milliseconds to wait for a fix | |
}, | |
watchId; // {Number} an ID to a watch timeout | |
// handles an error finding the user's position | |
function onError(error) { | |
var message = error.message, | |
code = error.code; | |
switch(error.code) { | |
case error.UNKNOWN_ERROR: | |
break; | |
case error.PERMISSION_DENIED: | |
break; | |
case error.POSITION_UNAVAILABLE: | |
break; | |
case error.TIMEOUT: | |
break; | |
} | |
} | |
// handles position data once found | |
function onPosition (position) { | |
var timestamp = position.timestamp, // {Date} | |
coords = position.coords, // {Object} placeholder | |
latitude = coords.latitude, // {Number} decimal degrees | |
longitude = coords.longitude, // {Number} decimal degrees | |
accuracy = coords.accuracy, // {Number} meters | |
altitudeAccuracy = coords.altitudeAccuracy, // {Number|null} meters | |
heading = coords.heading, // {Number|null} degrees relative to true north | |
speed = coords.speed; // {Number|null} meters per second | |
coords = null; | |
} | |
// detects if geolocation is supported | |
if ( ! Geo ) { throw new Error("geolocation not available."); } | |
// Gets the current position once | |
Geo.getCurrentPosition(onPosition, onError, options); | |
// Returns the current position, then watches for change in position | |
watchId = Geo.watchPosition(onPosition, onError, options); | |
// Stops watching for position changes | |
Geo.clearWatch(watchId); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment