我已经尝试这样做了几天但还没到任何地方。
这适用于使用Phonegap Build构建的混合应用程序。
我正在尝试使用我的地理定位应用来计算出行进的距离。到目前为止,我已经拥有了它,所以它返回A和B之间的值,但我需要它来解决当前位置更新(观察位置)所以它反复迭代,所以如果我进行了10次循环它就不会#39 ; t只是圆的半径。
这是我到目前为止所拥有的...... 请原谅代码,我对Javascript相对较新,并且很难理解参数并完全返回。
Starting Latitude: <span id="starting_lat">0</span><br>
Starting Longitude: <span id="starting_long">0</span><br>
Current Latitude: <span id="current_lat">0</span><br>
Current Longitude: <span id="current_long">0</span><br>
Speed: <span id="speed">0</span><br>
My Distance: <span id="myDistance">0</span><br>
<script>
var startingLat;
var startingLong;
var currentLat;
var currentLong;
startApp(); //Start the Application
function startApp() {
getStartingPosition(); //Get my starting position
startTracking(); // Start watching the location
}
function getStartingPosition(){
var getStartingLocation = function(position) {
startingLat=position.coords.latitude;
startingLong=position.coords.longitude;
document.getElementById('starting_lat').innerHTML=(startingLat);
document.getElementById('starting_long').innerHTML=(startingLong);
};
// onError Callback receives a PositionError object
//
function displayError(frror) {
alert('code: ' + frror.code + '\n' +
'message: ' + frror.message + '\n');
}
navigator.geolocation.getCurrentPosition(getStartingLocation, displayError);
}
function startTracking(){
function onSuccess(position) {
currentLat=position.coords.latitude;
currentLong=position.coords.longitude;
document.getElementById('current_lat').innerHTML=(currentLat);
document.getElementById('current_long').innerHTML=(currentLong);
getDistance()
}
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
// Options: throw an error if no update is received every 30 seconds.
//
var watchID = navigator.geolocation.watchPosition(onSuccess, onError, { timeout: 30000 });
}
// Take the starting longitude and latitude, along with current latitude and longitude to work out the distance and output it to the myDistance div.
function getDistance(){
var lat1 = startingLat;
var lon1 = startingLong;
var lat2 = currentLat;
var lon2 = currentLong;
var R = 6371; // Radius of the earth in km
var dLat = deg2rad(lat2-lat1); // deg2rad below
var dLon = deg2rad(lon2-lon1);
var a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2)
;
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c; // Distance in km
d=d.toFixed(2);
document.getElementById('myDistance').innerHTML=(d);
function deg2rad(deg) {
return deg * (Math.PI/180)
}
}
</script>
我尝试在这里使用另一个例子来添加一个数组的距离,但结构与我所做的略有不同,所以我发现它令人困惑。任何人都可以推荐一种简单的方法来应用迭代距离的数组吗?
再次提一下,这是针对使用Phonegap Build的移动应用程序。
谢谢。
答案 0 :(得分:0)
This page by Movable Type包含地理空间计算的公式,更好的是,大多数公式已经written in Javascript。
所以使用他们的库和你的例子,我会做这样的事情:
Starting Latitude: <span id="starting_lat">0</span><br>
Starting Longitude: <span id="starting_long">0</span><br>
Current Latitude: <span id="current_lat">0</span><br>
Current Longitude: <span id="current_long">0</span><br>
Speed: <span id="speed">0</span><br>
Direct Distance (km): <span id="directDistance">0</span><br>
Cumulative Distance (km): <span id="cumulativeDistance">0</span><br>
<script>
var startPos;
var prevPos;
var currentPos;
var watchID;
var cumulativeDist = 0;
document.addEventListener("deviceready", startApp, false); //Start the Application
function startApp() {
// Start watching the location
watchID = navigator.geolocation.watchPosition(onSuccess, onError, {timeout: 30000, maxAge:0, enableHighAccuracy: false }); // enableHighAccuracy=false => No GPS
}
function onSuccess(position) {
if(currentPos){
prevPos = currentPos;
)
currentPos = new LatLon(position.coords.latitude, position.coords.longitude);
document.getElementById('current_lat').innerHTML=(currentPos.lat());
document.getElementById('current_long').innerHTML=(currentPos.lon());
if(!startPos){
startPos = currentPos;
document.getElementById('starting_lat').innerHTML=(startPos.lat());
document.getElementById('starting_long').innerHTML=(startPos.lon())
}
if(prevPos){
getDistance();
}
}
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
function getDistance(){
cumulativeDist += currentPos.distanceTo(prevPos);
document.getElementById('cumulativeDistance').innerHTML=(cumulativeDist);
var directDist = currentPos.distanceTo(startPos);
document.getElementById('directDistance').innerHTML=(directDist);
}
</script>