Variable startPos
is defined as local inside
navigator.geolocation.getCurrentPosition(function(position) {
var startPos = position;
...
});
It is not visible outside that part of code:
navigator.geolocation.watchPosition(function(position) {
document.getElementById("currentLat").innerHTML = position.coords.latitude;
document.getElementById("currentLon").innerHTML = position.coords.longitude;
document.getElementById("distance").innerHTML =
calculateDistance(startPos.coords.latitude, startPos.coords.longitude, position.coords.latitude, position.coords.longitude);
});
}
and you get error:
Uncaught ReferenceError: startPos is not defined
startPos
variable has to be defined at beginning of onload
function:
window.onload = function() {
var startPos;
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
//var startPos = position;
startPos = position;
...