|
var Where = { |
|
|
|
// Utils |
|
|
|
debug: function (s) { |
|
document.getElementById("debug").innerHTML += "debug: " + s + "\n"; |
|
}, |
|
|
|
error: function (s) { |
|
document.getElementById("debug").innerHTML += "ERROR: " + s + "\n"; |
|
}, |
|
|
|
// Events |
|
|
|
onLoad: function() { |
|
"use strict"; |
|
Where.debug('application started.'); |
|
if (navigator.geolocation) { |
|
Where.debug('Geo Location API is supported.'); |
|
// navigator.geolocation.getCurrentPosition(success, error); |
|
Where.watchid = navigator.geolocation.watchPosition( |
|
Where.watchPosition, |
|
Where.watchError, |
|
Where.watchOption |
|
); |
|
Where.debug('watchPosition returns ' + Where.watchid); |
|
} else { |
|
Where.error('Geo Location API is not supported.'); |
|
} |
|
document.getElementById("append").onclick = Where.onAppend; |
|
document.getElementById("clear").onclick = Where.onClear; |
|
document.getElementById("register").onclick = Where.onRegister; |
|
}, |
|
|
|
onClear: function (e) { |
|
Where.records = []; |
|
Where.debug('Cleared.'); |
|
localStorage.setItem('records', JSON.stringify(Where.records)); |
|
Where.updateDisplay(); |
|
}, |
|
|
|
onAppend: function (e) { |
|
var now = new Date(); |
|
Where.appendRecord({ |
|
getMilliseconds: now.getMilliseconds(), |
|
toLocaleString: now.toLocaleString() |
|
}); |
|
}, |
|
|
|
onRegister: function (e) { |
|
if (Where.records === null) { |
|
Where.error("onRegister: No records."); |
|
} else if (Where.records.length == 0) { |
|
Where.error("onRegister: No records."); |
|
} else { |
|
var name = document.getElementById("place").value; |
|
var lat = Where.records[0].latitude; |
|
var lon = Where.records[0].longitude; |
|
Where.debug("onRegister: name = " + name); |
|
Where.debug("onRegister: lat = " + lat); |
|
Where.debug("onRegister: lon = " + lon); |
|
Where.appendPlace({ |
|
name: name, |
|
latitude: lat, |
|
longitude: lon, |
|
}); |
|
} |
|
}, |
|
|
|
// View |
|
|
|
updateDisplay: function () { |
|
document.getElementById("records").innerHTML = Where.records.map(function(obj) { |
|
return "<li>" + JSON.stringify(obj) + "</li>"; |
|
}).join(''); |
|
document.getElementById("places").innerHTML = Where.places.map(function(obj) { |
|
return "<li>" + JSON.stringify(obj) + "</li>"; |
|
}).join(''); |
|
}, |
|
|
|
// Model |
|
|
|
records: [], |
|
places: [], |
|
|
|
appendRecord: function (obj) { |
|
Where.records = JSON.parse(localStorage.getItem('records')); |
|
if (Where.records === null) { |
|
Where.records = []; |
|
} |
|
Where.records.unshift(obj); |
|
// Where.debug("appendRecord: " + JSON.stringify(obj)); |
|
localStorage.setItem('records', JSON.stringify(Where.records)); |
|
Where.updateDisplay(); |
|
}, |
|
|
|
appendPlace: function (obj) { |
|
Where.places = JSON.parse(localStorage.getItem('places')); |
|
if (Where.places === null) { |
|
Where.places = []; |
|
} |
|
Where.places.unshift(obj); |
|
Where.debug("appendPlace: " + JSON.stringify(obj)); |
|
localStorage.setItem('places', JSON.stringify(Where.places)); |
|
Where.updateDisplay(); |
|
}, |
|
|
|
// Geolocation |
|
|
|
watchid: -1, |
|
|
|
watchPosition: function (pos) { |
|
var now = new Date(); |
|
var lat = pos.coords.latitude; |
|
var lon = pos.coords.longitude; |
|
Where.debug('(latitude, longitude) = (' + lat + ', ' + lon + ')'); |
|
Where.appendRecord({ |
|
time: now.getTime(), |
|
latitude: lat, |
|
longitude: lon, |
|
timeISOString: now.toISOString() |
|
}); |
|
}, |
|
|
|
watchError: function (error) { |
|
Where.error('watchError: ' + error.code + ': ' + error.message); |
|
switch (error.code) { |
|
case 1: |
|
alert('watchPosition: PERMISSION_DENIED'); |
|
break; |
|
case 2: |
|
alert('watchPosition: POSITION_UNAVAILABLE'); |
|
break; |
|
case 3: |
|
// alert('watchPosition: TIMEOUT'); |
|
break; |
|
default: |
|
alert('watchPosition: UNKNOWN_ERROR'); |
|
} |
|
}, |
|
|
|
watchOption: { |
|
enableHighAccuracy: false, |
|
timeout: 3000, |
|
maximumAge: 2000 |
|
}, |
|
|
|
terminator: null |
|
}; |
|
|
|
window.onload = Where.onLoad; |