2012-01-31 117 views
0

我只是想分配position.coords.latitude和經度來經緯度和經度,但似乎我失去了一些東西,因爲控制檯總是說緯度經緯度未定義。卡住的JavaScript變量關閉

function init() 
{ 
    var lat; 
    var lon; 

    if (navigator.geolocation) { 

     navigator.geolocation.getCurrentPosition(function(position) 
     { 
      lat = position.coords.latitude; 
      lon = position.coords.longitude;  
     }); 
    } 

    console.log(lat); 
    console.log(lon); 

} 
+0

是getCurrentPosition執行異步? (setTimeout/setInterval/AJAX) – TimWolla 2012-01-31 12:11:32

+0

是異步。對不起,因爲noob,但我怎麼能讓它同步? – 2012-01-31 12:15:37

+0

當沒有參數時:除修改腳本外沒有辦法。但可能這是第三方庫,對吧? – TimWolla 2012-01-31 12:20:51

回答

2

的問題是,當你喜歡你先做定義與var變量,它們的值是不確定的。現在,當您調用getCurrentPosition函數時,它可能是異步的,這意味着console.logs在您爲其分配任何值之前被調用。請嘗試以下變化

function init() 
{ 
    var lat; 
    var lon; 

    if (navigator.geolocation) { 

     navigator.geolocation.getCurrentPosition(function(position) 
     { 
      lat = position.coords.latitude; 
      lon = position.coords.longitude; 

      // Log them in the callback function 
      console.log(lat); 
      console.log(lon); 
     }); 
    } 

} 

既然你想你有COORDS後執行實際代碼,這裏是你如何可以改變你的初始化函數,以適應異步架構。

// New function argument callbackFn - the function to call after 
// coords are loaded 
function init(callbackFn) 
{ 
    // Removed local variables, no need for them here 

    if (navigator.geolocation) { 

     navigator.geolocation.getCurrentPosition(function(position) 
     { 
      var lat = position.coords.latitude; 
      var lon = position.coords.longitude; 

      // This will be logged after the async call completes, probably after 
      // the console.log call below 
      console.log("finished async call"); 

      // Execute the callback with coords 
      callbackFn(lat, lon); 
     }); 
    } 

    // This will be called as soon as JS steps over previous lines, 
    // but before the asynchronous request has completed 
    console.log("finished init"); 
} 

讓我們假裝你的實際程序開始於你執行一個叫做經度和緯度開始的函數。在這種情況下,你可以使用下面的啓動程序:

function start(latitude, longitude) { 
    // ... SNIP ... DO SOMETHING COOL 
} 

init(start); 
+0

是的函數getCurrentPosition是異步的,但我想要實現的是將座標分配給緯度和經度變量,以便之後使用它們 – 2012-01-31 12:14:18

+0

@AssadUllah異步架構是Javascript的基本思想之一,試圖強制它進入同步執行是一個糟糕的主意,所以你總是必須使用回調函數。用例子更新了答案,你可以做到這一點。 – zatatatata 2012-01-31 12:33:04

+0

@AssadUllah如果你願意,你也可以通過將'var lat'改成'window.lat'(或者只是'lat',但我建議使用'window'來防止混淆)將變量寫成全局變量。請記住,在異步回調已經返回之前,您仍然無法使用它們,所以您應該繼續沿着我展示的方向行事。 – zatatatata 2012-01-31 12:37:55

1

我會想象navigator.geolocation.getCurrentPosition()是異步的,所以它不會有通過你嘗試寫到控制檯時執行。

嘗試移動你安慰寫入回調函數:

function init() 
{ 
    if (navigator.geolocation) { 

     navigator.geolocation.getCurrentPosition(function(position) 
     { 
      var lat = position.coords.latitude; 
      var lon = position.coords.longitude; 

      console.log(lat); 
      console.log(lon); 
     }); 
    } 
} 
+0

是的函數getCurrentPosition是異步的,但我想要實現的是將座標分配給緯度和經度變量,所以我可以在以後使用它們 – 2012-01-31 12:16:57

+0

這很好,只需在全局範圍聲明lat和lon,並將它們的值分配給回電話。您可能需要從回調方法引導任何進一步的邏輯 - 也許讓它調用一個名爲handleGeolocation()或類似的方法。 – 2012-01-31 12:23:33