将变量从一个函数传递到另一个函数javascript
(我在谷歌地图项目上工作) 我试图将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();
只是删除var
:
function showPosition(position) {
latlon = position.coords.latitude + "," + position.coords.longitude;
/*document.write (latlon); */
}
事实是,当你用的关键词var
定义一个变量,你作出这样的局部变量对于范围和全球所有内部功能。
您已经将latlon
定义为全局变量。当你把var
放在另一个函数之前时,它将在函数范围内是局部的。所以,它不会影响全球。
还有一个问题,即OP在明确调用showPosition()时可能会测试它,它没有被通过一个位置... – Code4aliving
我不能删除var。 showLosition函数内部的var latlon用于getLocation函数。 –
@AlexVandyke,我在你放的代码中看到的,你可以安全地从函数中删除'var'。如果您输入汇总代码,请使用完整代码更新问题。 – Mojtaba
你声明了一个全局变量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); */
}
我无法移除var。 showLosition函数内部的var latlon用于getLocation函数 –
那么'showPosition()'不会调用'hello()',并且'hello()'没有设置为接收任何传入参数。 'latlon'是'showPosition()'的局部变量,即仅在该范围内可引用。你需要传递它,或者将它写入全局可访问的东西。 – Utkanos
当你调用showPosition()时,它没有被传递它的'position'参数。另外,showPosition()声明了一个名为'latlon'的LOCAL变量,这意味着它不会设置你的全局'latlon'变量。这意味着hello()函数将访问未触及的全局“latlon”变量。 – Code4aliving
请注意,您[绝对不能使用'document.write'](http://stackoverflow.com/q/802854/1048572) – Bergi