Skip to content

Instantly share code, notes, and snippets.

@filmaj
Created September 15, 2017 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filmaj/89a30995504952dabd686d5fd79e96e5 to your computer and use it in GitHub Desktop.
Save filmaj/89a30995504952dabd686d5fd79e96e5 to your computer and use it in GitHub Desktop.
app.js
var app = {
// Application Constructor
initialize: function() {
document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
},
// deviceready Event Handler
//
// Bind any cordova events here. Common events are:
// 'pause', 'resume', etc.
onDeviceReady: function() {
this.receivedEvent('device is ready');
var first_options = { enableHighAccuracy: true, maximumAge: 300000, timeout: 10000 };
var second_options = { enableHighAccuracy: false, maximumAge: 300000, timeout: 6000 };
var did_second = false;
var second_attempt = function() {
if (!did_second) {
did_second = true;
navigator.geolocation.getCurrentPosition(function(position) {
app.receivedEvent('second success!');
alert('second! Latitude: ' + position.coords.latitude + '\n' +
'Longitude: ' + position.coords.longitude + '\n' +
'Altitude: ' + position.coords.altitude + '\n' +
'Accuracy: ' + position.coords.accuracy + '\n' +
'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '\n' +
'Heading: ' + position.coords.heading + '\n' +
'Speed: ' + position.coords.speed + '\n' +
'Timestamp: ' + position.timestamp + '\n');
},
function onError(error) {
app.receivedEvent('second FAIL');
alert('second! code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}, second_options);
}
};
navigator.geolocation.getCurrentPosition(function(position) {
app.receivedEvent('first success!');
alert('first! Latitude: ' + position.coords.latitude + '\n' +
'Longitude: ' + position.coords.longitude + '\n' +
'Altitude: ' + position.coords.altitude + '\n' +
'Accuracy: ' + position.coords.accuracy + '\n' +
'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '\n' +
'Heading: ' + position.coords.heading + '\n' +
'Speed: ' + position.coords.speed + '\n' +
'Timestamp: ' + position.timestamp + '\n');
second_attempt();
},
function onError(error) {
app.receivedEvent('first FAIL');
alert('first! code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
second_attempt();
}, first_options);
setTimeout(function() {
}, 30000);
},
// Update DOM on a Received Event
receivedEvent: function(evt) {
var parentElement = document.getElementById('deviceready');
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
receivedElement.innerHTML = evt;
console.log('Received Event: ' + evt);
}
};
app.initialize();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment