123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- 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.');
- }
|