Skip to content

Instantly share code, notes, and snippets.

@bjhaid
Last active December 23, 2015 14:49
Show Gist options
  • Save bjhaid/6651308 to your computer and use it in GitHub Desktop.
Save bjhaid/6651308 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<title>Device Properties Example</title>
<script type="text/javascript" charset="utf-8" src="cordova-1.9.0.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for Cordova to load
//
document.addEventListener("deviceready", onDeviceReady, false);
var watchID = null;
var timerID = null;
var globalPosition = null;
// Cordova is ready
//
function onDeviceReady() {
var options = { maximumAge: 3000, timeout: 5000, enableHighAccuracy: true };
watchID = navigator.geolocation.watchPosition(onSuccess, onError, locOptions);
timerID = setInterval ( updateLocation, 100 );
}
function getLocation() {
navigator.geolocation.getCurrentPosition(onSuccess, onError,options);
}
function updateLocation () {
var position = globalPosition;
if (position)
{
alert(position.coords);
}
}
// onSuccess Geolocation
//
function onSuccess(position) {
globalPosition = position;
alert("SUCESSFUL!");
}
// clear the watch that was started earlier
//
function clearWatch() {
if (watchID != null) {
navigator.geolocation.clearWatch(watchID);
watchID = null;
}
}
// onError Callback receives a PositionError object
//
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
</script>
</head>
<body>
<p id="geolocation">Watching geolocation...</p>
<button onclick="clearWatch();">Clear Watch</button>
<button onclick="getLocation();">Get Location</button>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment