2012-08-30 108 views
1

這個函數是異步的,在這種情況下,這是我的大部分問題。地理位置和計算從當前經緯度到另一經度緯度的距離

我想獲取我的位置的當前經度和緯度,這樣我就可以在distanceFromCurrent函數中使用它們來計算我當前位置和給定的點之間的距離。

一切工作正常,除了我不能在其異步功能外使用currLatcurrLong

function getCurrentPosition(){ 
    navigator.geolocation.getCurrentPosition(getCoords, getError); 
} 

function getCoords(position){ 
    var currLat = position.coords.latitude; 
    var currLon = position.coords.longitude; 
} 

function getError(error) { 
    alert("Error"); 
} 

// convert degrees to radians 
Number.prototype.toRad = function() 
{ 
    return this * Math.PI/180; 
} 

這是計算與一組緯度/經度從georss和正常工作的距離當前的經緯度像它此刻的功能。

function distanceFromCurrent(georss) 
{ 
    getCurrentPosition(); 
    var currLat = 3.0; 
    var currLon = 4.0; 

    georss = jQuery.trim(georss); 
    var pointLatLon = georss.split(" "); 
    var pointLat = parseFloat(pointLatLon[0]); 
    var pointLon = parseFloat(pointLatLon[1]); 

    var R = 6371;     //Radius of the earth in Km    
    var dLat = (pointLat - currLat).toRad(); //delta (difference between) latitude in radians 
    var dLon = (pointLon - currLon).toRad(); //delta (difference between) longitude in radians 

    currLat = currLat.toRad();   //conversion to radians 
    pointLat = pointLat.toRad(); 

    var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(currLat) * Math.cos(pointLat); 
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); //must use atan2 as simple arctan cannot differentiate 1/1 and -1/-1 
    var distance = R * c; //sets the distance 

    distance = Math.round(distance*10)/10;  //rounds number to closest 0.1 km 
    return distance; //returns the distance 
} 

因此,沒有人有一個想法/解決方案可能獲得該緯度/經度不同的方式或我要對這個完全錯誤的?

+0

您是否找到解決方案?把你的函數放在回調函數中,不幸的是javascript如何工作 – 2012-08-30 20:00:34

回答

0

參數添加在您的distanceFromCurrent功能,並調用它inyour回調,其中的GeoRSS是一個全局變量:

var georss = "43.12 7.12"; 

function getCoords(position){ 
    var currLat = position.coords.latitude; 
    var currLon = position.coords.longitude; 
    var distance = distanceFromCurrent(lat, lon, georss); 
} 

function distanceFromCurrent(georss, currLat, currLon){ 
    // getCurrentPosition(); 
    // var currLat = 3.0; 
    // var currLon = 4.0; 
    georss = jQuery.trim(georss); 
    var pointLatLon = georss.split(" "); 
//.... 
-1

getCorrentPosition是異步的。 您需要發送一個指向將與數據一起使用的funcion的指針作爲第一個參數。 示例

navigator.geolocation.getCurrentPosition(function(position) { 
    var latitude = position.coords.latitude 
    var longitude = position.coords.longitude; 
    distanceFromCurrent(currLatitude, currLongitude, latitude, longitude); 
}); 

這應該是您開始的好方法。

問題是你正試圖以錯誤的方式解決你的問題。你應該調用getCurrentPosition並處理它裏面的所有返回的數據。 也許這樣的事情(對你的函數做一些修改)就可以實現。

請記住,JavaScript的不夜城(沒有休眠),這樣所有能不能做到「現在」需要被推遲。這是這種方法發生的。您需要考慮將回調函數發送給函數而不是返回。因爲我沒有你所有的代碼,我不知道如何改變它。但基本上,你需要:

function distanceFromCurrent(georss, callback) 
{ //... 

你可以看看這裏的完整信息: https://developer.mozilla.org/en-US/docs/Using_geolocation

+0

爲什麼我得到一個'-1'? – brunoais

0

以下呼叫被封鎖沒有那麼這將無法工作。

navigator.geolocation.getCurrentPosition(getCoords, getError); 

的方式您的通話,這意味着getCurrentPosition的結果()不會有你達到distanceFromCurrent()方法的年底前完成。

相反,您可以執行以下操作並調用update()而不是distanceFromCurrent()。這將最終調用您的distanceFromCurrent()方法與當前的經緯度值。

注:以下是僞代碼,我懷疑它會正確地傳遞你的GeoRSS值。

update() 
{ 
getCurrentPosition(); 
} 
function getCurrentPosition(){ 
navigator.geolocation.getCurrentPosition(getCoords, getError); 
} 

function getCoords(position){ 
var currLat = position.coords.latitude; 
var currLon = position.coords.longitude; 
distanceFromCurrent(georss, lat, lng); 
} 

function distanceFromCurrent(georss, lat, lng) 
{ 
    // Remove -> getCurrentPosition(); 
    var currLat = lat; 
    var currLon = lng; 

    georss = jQuery.trim(georss); 
    ... 
} 
相關問題