69 lines
1.8 KiB
JavaScript
69 lines
1.8 KiB
JavaScript
function isGeoAvailable() {
|
|
"use strict";
|
|
return navigator.geolocation;
|
|
}
|
|
|
|
function setMessage(message) {
|
|
"use strict";
|
|
var geo = document.getElementById("geo");
|
|
if (geo) {
|
|
geo.innerHTML = message;
|
|
}
|
|
else {
|
|
console.log("setMessage()", message);
|
|
}
|
|
}
|
|
|
|
function getPositionMessage(position) {
|
|
"use strict";
|
|
|
|
var coords = position.coords;
|
|
|
|
var date = new Date(position.timestamp);
|
|
|
|
var message = "<p>Position: </p>\n" +
|
|
"<ul>\n" +
|
|
" <li>Timestamp: " + position.timestamp + " = " + date.toLocaleString() + "</li>\n" +
|
|
" <li>Accuracy: " + coords.accuracy + " meters</li>\n" +
|
|
" <li>Latitude: " + coords.latitude + "</li>" +
|
|
" <li>Longitude: " + coords.longitude + "</li>" +
|
|
" <li>Altitude: " + (coords.altitude ? coords.altitude : "unknown") + "</li>" +
|
|
" <li>Altitude Accuracy: " + (coords.altitudeAccuracy ? coords.altitudeAccuracy : 0) + "</li>" +
|
|
" <li>Heading: " + (coords.heading ? coords.heading : "unknown") + "</li>\n" +
|
|
" <li>Speed: " + (coords.speed ? coords.speed : "unknown") + "</li>\n"
|
|
" </ul>\n";
|
|
|
|
return message;
|
|
}
|
|
|
|
function showPositionMessage(position) {
|
|
"use strict";
|
|
setMessage("showPositionMessage()", getPositionMessage(position));
|
|
}
|
|
|
|
function showPositionError(error) {
|
|
"use strict";
|
|
var reason;
|
|
switch (error.code) {
|
|
case error.POSITION_UNAVAILABLE:
|
|
reason = "Position unavailable";
|
|
break;
|
|
case error.PERMISSION_DENIED:
|
|
reason = "Permission denied";
|
|
break;
|
|
case error.TIMEOUT:
|
|
reason = "Timeout"
|
|
break;
|
|
default:
|
|
reason = "Unknown position error (" + error.code + ")";
|
|
break;
|
|
}
|
|
setMessage(reason + ": " + error.message);
|
|
}
|
|
|
|
if (isGeoAvailable()) {
|
|
navigator.geolocation.getCurrentPosition(showPositionMessage, showPositionError);
|
|
}
|
|
else {
|
|
setMessage('Geolocation API is not available.');
|
|
}
|