将变量从一个函数传递给另一个函数

时间:2017-03-15 20:24:14

标签: javascript function

(我在谷歌地图项目上工作) 我试图将latlon变量形式函数showPosition传递给hello函数,但我不能。

当我在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();

2 个答案:

答案 0 :(得分:1)

只需删除var

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

事实上,无论何时通过关键短语var定义变量,都可以为该范围创建该变量本地变量,并为所有内部函数创建全局变量。

您已将latlon定义为全局变量。当你将var放在另一个函数中之前,它将在函数范围内是局部的。所以,它不会影响全球的。

答案 1 :(得分: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); */
 }