我正在使用变量进行计算,但是当我在console.log中查看结果时,我得到了操作中两个变量之一的值。 我认为你必须转换为数字,但不知道该怎么做 不起作用的代码是“if”(工作但不好,结果不正确)
var tot = 0;
var media = 0;
var tempo = 0;
(function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
pos1 = position.coords.latitude;
pos11 = position.coords.longitude;
console.log('latitudine ' + pos1);
console.log('longitudine ' + pos11);
//Passano 2 secondi
setTimeout(function() {
pos2 = 45.2968571;
pos22 = 12.034978499999966;
console.log('latitudine ' + pos2);
console.log('longitudine ' + pos22);
alert("Sono passati 2 secondi");
}, 2000);
// Converte in radianti
setTimeout(function() {
if (typeof(Number.prototype.toRad) === "undefined") {
Number.prototype.toRad = function() {
return this * Math.PI / 180;
}
}
//
// Calcolo la Distanza
var R = 6371; // km
var dLat = (pos2 - pos1).toRad();
var dLon = (pos22 - pos11).toRad();
var lat1 = pos1.toRad();
var lat2 = pos2.toRad();
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var distance1 = R * c;
var distance2 = distance1.toFixed(2); //Porto decimali
var metri2 = (distance2 * 1000); // Porto in metri
tot += metri2; //somma teorica distanza
console.log(distance2, tot);
alert('hai percorso: ' + tot + 'm ' + tot);
var distance2 = localStorage.setItem('key', distance2);
var t1 = (2 / 3600); // h costante
var spazio = (metri2 / 1000); // Km
var velocità = spazio / t1; // hm/h
console.log('Stai andando a: ' + velocità);
media += velocità; //Somma velocità per la media
velocitàtotale = media * 1;
var temp = 2; //Variabile tempo
tempo += temp; //quanto tempo é passato
tempototale = tempo * 1;
console.log('Quanto tempo è passato: ' + tempo);
if (tempo >= 20) {
var time = tempototale / 2;
var medtot = velocitàtotale / time;
console.log('media: ' + medtot);
};
}, 2000);
})
}
setTimeout(arguments.callee, 10000);
})();