2017-03-15 207 views
0

(我在谷歌地圖項目上工作) 我試圖將latlon變量窗體函數showPosition傳遞給hello函數,但是我不能。將變量從一個函數傳遞到另一個函數javascript

當我在showPosition函數中打印latlon時,一切正常。 當我在hello函數上打印latlon時,我得到空白頁面。

var latlon; 
 

 
window.onload = function getLocation() { 
 
    if (navigator.geolocation) { 
 
     navigator.geolocation.getCurrentPosition(showPosition, showError); 
 
    } else { 
 
     x.innerHTML = "Geolocation is not supported by this browser."; 
 
    } 
 
} 
 

 
function showPosition(position) { 
 
    var latlon = position.coords.latitude + "," + position.coords.longitude; 
 
    /*document.write (latlon); */ 
 
} 
 

 

 
function hello() { 
 
    variablelatlon = latlon; 
 
    document.write (variablelatlon);  
 
} 
 

 
showPosition(); 
 
hello(); 
 

 
function showError(error) { 
 
    switch(error.code) { 
 
     case error.PERMISSION_DENIED: 
 
      x.innerHTML = "User denied the request for Geolocation." 
 
      break; 
 
     case error.POSITION_UNAVAILABLE: 
 
      x.innerHTML = "Location information is unavailable." 
 
      break; 
 
     case error.TIMEOUT: 
 
      x.innerHTML = "The request to get user location timed out." 
 
      break; 
 
     case error.UNKNOWN_ERROR: 
 
      x.innerHTML = "An unknown error occurred." 
 
      break; 
 
    } 
 
}

我我的代碼改變了這一點。但我沒有看到任何結果。

var x = document.getElementById("demo"); 
 
var coordlat; 
 
var coordlng; 
 

 
window.onload = function getLocation() { 
 
    if (navigator.geolocation) { 
 
     navigator.geolocation.getCurrentPosition(showPosition, showError); 
 
    } else { 
 
     x.innerHTML = "Geolocation is not supported by this browser."; 
 
    } 
 
} 
 

 
function showPosition(position) { 
 
    var latlon = position.coords.latitude + "," + position.coords.longitude; 
 
    coordlat = position.coords.latitude; 
 
    coordlng = position.coords.longitude; 
 
} 
 

 

 
function hello() { 
 
    document.write (coordlat);  
 
    document.write (coordlng);  
 
} 
 

 

 
function showError(error) { 
 
    switch(error.code) { 
 
     case error.PERMISSION_DENIED: 
 
      x.innerHTML = "User denied the request for Geolocation." 
 
      break; 
 
     case error.POSITION_UNAVAILABLE: 
 
      x.innerHTML = "Location information is unavailable." 
 
      break; 
 
     case error.TIMEOUT: 
 
      x.innerHTML = "The request to get user location timed out." 
 
      break; 
 
     case error.UNKNOWN_ERROR: 
 
      x.innerHTML = "An unknown error occurred." 
 
      break; 
 
    } 
 
} 
 

 
showPosition(); 
 
hello();

+1

那麼'showPosition()'不會調用'hello()',並且'hello()'沒有設置爲接收任何傳入參數。 'latlon'是'showPosition()'的局部變量,即僅在該範圍內可引用。你需要傳遞它,或者將它寫入全局可訪問的東西。 – Utkanos

+0

當你調用showPosition()時,它沒有被傳遞它的'position'參數。另外,showPosition()聲明瞭一個名爲'latlon'的LOCAL變量,這意味着它不會設置你的全局'latlon'變量。這意味着hello()函數將訪問未觸及的全局「latlon」變量。 – Code4aliving

+0

請注意,您[絕對不能使用'document.write'](http://stackoverflow.com/q/802854/1048572) – Bergi

回答

1

只是刪除var

function showPosition(position) { 
    latlon = position.coords.latitude + "," + position.coords.longitude; 
    /*document.write (latlon); */ 
} 

事實是,當你用的關鍵詞var定義一個變量,你作出這樣的局部變量對於範圍和全球所有內部功能。

您已經將latlon定義爲全局變量。當你把var放在另一個函數之前時,它將在函數範圍內是局部的。所以,它不會影響全球。

+0

還有一個問題,即OP在明確調用showPosition()時可能會測試它,它沒有被通過一個位置... – Code4aliving

+0

我不能刪除var。 showLosition函數內部的var latlon用於getLocation函數。 –

+0

@AlexVandyke,我在你放的代碼中看到的,你可以安全地從函數中刪除'var'。如果您輸入彙總代碼,請使用完整代碼更新問題。 – Mojtaba

0

你聲明瞭一個全局變量latlon,但是通過在函數showPosition中重新聲明它來使其成爲本地的。這應該有所幫助:

var latlon; 

window.onload = function getLocation() { 
    if (navigator.geolocation) { 
     navigator.geolocation.getCurrentPosition(showPosition, showError); 
    } else { 
     x.innerHTML = "Geolocation is not supported by this browser."; 
    } 
} 
// You declared global variable laton but then make it local by redeclaring it in function showPosition 
function showPosition(position) { 
    /*var*/ latlon = position.coords.latitude + "," + position.coords.longitude; 
    /*document.write (latlon); */ 
} 
+0

我無法移除var。 showLosition函數內部的var latlon用於getLocation函數 –

相關問題