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("message:", 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(getPositionMessage(position)); } function showPositionError(error) { "use strict"; setMessage(error); } if (isGeoAvailable()) { navigator.geolocation.getCurrentPosition(showPositionMessage, showPositionError); } else { setMessage('Geolocation API is not available.'); }