2012-03-09 55 views
0

我有一個嚴重的問題,我一直在嘗試調試幾天。 我有一個腳本獲取用戶當前的經緯度,然後將它們存儲在變量中。然而,當我嘗試在這個函數之外和// init地圖區域中使用這些變量時,地圖只是沒有顯示出來。通過提醒我可以看到的位置函數變量被設置爲「未定義」的變量。這裏是我的代碼:變量傳遞不工作 - 值未定義

//main function here 
function initialize() { 
var lat; 
var lon; 

//check if user has geo feature 
if(navigator.geolocation){ 

navigator.geolocation.getCurrentPosition(
//get position 
function(position){ 
    lat = position.coords.latitude; 
    lon = position.coords.longitude; 
    }, 
// if there was an error 
function(error){ 
    alert('ouch'); 
}); 
} 
//case the users browser doesn't support geolocations 
else { 
alert("Your browser doesn't support geolocations, please consider downloading Google Chrome"); 
} 
//init map 
var myOptions = { 
    center: new google.maps.LatLng(lat, lon), 
    zoom: 16, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
var map = new google.maps.Map(document.getElementById("map_canvas"), 
     myOptions); 

} 

感謝您的幫助,林依晨

回答

4

那是因爲你在函數中聲明變量。這些變量對 initialize函數是私有的,只能在其中進行訪問。如果您需要能夠在初始化函數之外訪問變量,請將變量聲明移出函數。

var lat; 
var lon; 
function initialize() { 
... 

看看這個 MDN article關於JavaScript中的變量作用域。

UPDATE

翻翻代碼,我再次認識到,問題不變量的作用域,得到了由壓痕混淆。我對Geolocation API不熟悉,但我認爲問題可能在於navigator.geolocation.getCurrentPosition()是異步的,因爲它必須等待用戶允許網站獲取設備的位置。因此myOptions將在檢索實際位置之前分配 - 因此當分配myOptions時,lat & lng仍未定義。

試試這個:

//main function here 
function initialize() { 
var lat, lon, map, myOptions; 

//check if user has geo feature 
if(navigator.geolocation){ 

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

    //init map 
    myOptions = { 
     center: new google.maps.LatLng(lat, lon), 
     zoom: 16, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    map = new google.maps.Map(document.getElementById("map_canvas"), 
     myOptions); 
}, 
// if there was an error 
function(error){ 
    alert('ouch'); 
}); 
} 
//case the users browser doesn't support geolocations 
else { 
alert("Your browser doesn't support geolocations, please consider downloading Google Chrome"); 
} 
} 
+0

這不是我的問題,我的問題是,在「center:new google.maps.LatLng(lat,lon)」變量lat和lon顯然沒有任何valval。 – arielschon12 2012-03-09 17:10:59

+0

@ arielschon12如果你的'console.log(position.coords.latitude,position.coords.longitude);'在那個函數中,你會得到什麼? – 2012-03-09 17:39:14

+0

@ arielschon12更新了我的答案! – 2012-03-09 17:55:19

0

更多的修正對對方的回答。 範圍的問題不相關。如原始代碼latlon中所定義,在問題作者正在使用的上下文範圍內alert

下面是一個證明它的可運行示例。

function getPos(f) { 
    var position = new Object(); 
    position.coords = new Object(); 
    position.coords.latitude = 5; 
    position.coords.longitude = 10; 
    f(position);   
} 


function initialize() { 
    var lat; 
    var lon; 
    getPos(

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

      ); 

     alert(lat + " " + lon); 

} 


initialize(); //expected 5 & 10 

不管怎麼說,這似乎不是一個純粹的JS問題。這似乎與您使用的任何谷歌API的問題。這個問題應該被標記爲這樣,因爲我不知道這個API或者你是否錯誤地調用它。