嗨,我正在通过将Javascript与地理位置结合使用来创建步行跟踪器网络应用。除了必须使用旧的latlong和新的latlong来计算距离的那部分以外,其他所有东西都工作正常。
代码:
function updateLocation(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
var accuracy = position.coords.accuracy;
var timestamp = position.timestamp;
document.getElementById("latitude").innerHTML = latitude;
document.getElementById("longitude").innerHTML = longitude;
document.getElementById("accuracy").innerHTML = accuracy.toFixed(2) + " meter";
document.getElementById("timestamp").innerHTML = new Date(timestamp).toLocaleString();
if (accuracy >= 500) {
updateStatus("Need more accurate values to calculate distance.");
return;
}
if ((lastLat != null) && (lastLong != null)) {
var currentDistance = distance(latitude, longitude, lastLat, lastLong);
document.getElementById("currDist").innerHTML =
"Current distance traveled: " + currentDistance.toFixed(2) + " km";
totalDistance += currentDistance;
document.getElementById("totalDist").innerHTML =
"Total distance traveled: " + currentDistance.toFixed(2) + " km";
}
lastLat = latitude;
lastLong = longitude;
updateStatus("Location successfully updated.");
}
添加了距离功能*
Number.prototype.toRadians = function() {
return this * Math.PI / 180;
}
function distance(latitude1, longitude1, latitude2, longitude2) {
// R is the radius of the earth in kilometers
var R = 6371;
var deltaLatitude = (latitude2-latitude1).toRadians();
var deltaLongitude = (longitude2-longitude1).toRadians();
latitude1 = latitude1.toRadians(), latitude2 = latitude2.toRadians();
var a = Math.sin(deltaLatitude/2) *
Math.sin(deltaLatitude/2) +
Math.cos(latitude1) *
Math.cos(latitude2) *
Math.sin(deltaLongitude/2) *
Math.sin(deltaLongitude/2);
var c = 2 * Math.atan2(Math.sqrt(a),
Math.sqrt(1-a));
var d = R * c;
return d;
}
希望有人能发现我做错了什么。 谢谢
更新:
我发现了为什么地理位置不能更新我的距离的问题..因为我声明的变量 lastlong 和 lastlat 没有给我以前的位置,它为我提供了纬度和经度的当前位置,这些位置是我在updateLocation中声明的,并为这些变量分配了lastlat和last long。
我的问题是现在我该如何使用 新位置,找不到任何解决方案。
新更新:
找到了解决此问题的方法。用于计算先前 距离我将坐标存储在数组中并获得最后的位置 计算当前位置的距离。
代码:
var coords = []; var distance = 0.0;
function calculateDistance(fromPos, toPos) {
var radius = 6371;
var toRad = function(number) {
return number * Math.PI / 180;
};
var latDistance = toRad(toPos.latitude - fromPos.latitude);
var lonDistance = toRad(toPos.longitude - fromPos.longitude);
var a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2) +
Math.cos(toRad(fromPos.latitude)) * Math.cos(toRad(toPos.latitude)) *
Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
return radius * (2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)));
}
var lastPos = coords[coords.length-1];
if(lastPos) {
distance += calculateDistance(lastPos, position.coords);
document.getElementById("currDist").innerHTML =
"Current distance traveled: " + distance.toFixed(2) + " km";
}
coords.push(position.coords);
我不明白的一件事是为什么数组中的时间戳未定义?因为经久耐用,我可以轻松自在地生活。
答案 0 :(得分:0)
有关完整的工作旅程记录器示例,请参见Google Drive或GitHub。
请注意TravelManagerPolyfil.js中的filterLocation函数,因为您可能只是获得许多零距离读数,并且需要限制watchPosition。
此代码有效:-
<!DOCTYPE html>
<html>
<body>
<p>Click the button to get your coordinates.</p>
<button onclick="getLocation()">Try It</button>
<p id="demo"></p>
<script>
var perthLat = 31.9505;
var perthLon = 115.8605;
var freoLat = 32.0569;
var freoLon = 115.7439;
const EARTH_RADIUS = 6378137;
var toRad =
function (num) {
return num * Math.PI / 180;
};
var calculateDistance =
function(lat1, lon1, lat2, lon2){
var dLat = toRad(lat2 - lat1);
var dLon = toRad(lon2 - lon1);
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(this.toRad(lat1)) *
Math.cos(toRad(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
var distance = EARTH_RADIUS * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return distance;
}
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Distance = " +
calculateDistance(perthLat, perthLon,
freoLat, freoLon) / 1000;
}
</script>
</body>
</html>