我一直在使用Haversine公式來處理這個HTML/Javascript代碼,以便告訴兩個設備之間的距離。目前尚未完成,但代碼應該可讀。由於某些原因,「d」距離的最終變量在計算後不是打印(document.write)。該網站目前在http://magebattle.netau.net/,但爲了使這篇文章乾淨,請查看源代碼,而不是我把它放在這裏。 (對不起壓痕) 相關JS:HTML座標距離計算器不打印
// Wait for device API libraries to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available
//
function onDeviceReady() {
navigator.geolocation.getCurrentPosition(onSuccess, onError);
}
// onSuccess Geolocation
//
function onSuccess(position) {
var lat2 = position.coords.latitude
var lon2 = position.coords.longitude
var lat1 = 42.806911;
var lon1 = -71.290611;
var R = 6371; // km
//has a problem with the .toRad() method below.
var x1 = lat2-lat1;
var dLat = x1.toRad();
var x2 = lon2-lon1;
var dLon = x2.toRad();
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
document.write(d);
}
// onError Callback receives a PositionError object
//
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
Number.prototype.toRad = function() {
return this * Math.PI/180;
}
顯示一些相關的js – depperm