JavaScript-显示为未定义的全局变量
有这么多关于这个话题的线索,但我仍然无法找到一个适合我的案例的答案 以下是我想做的: 1) 使用Google Maps API v3定义获取地理代码的函数 2) 使用相关地址调用函数,以获取此特定地址的地理代码 如果我将JavaScript-显示为未定义的全局变量,javascript,google-maps-api-3,global-variables,undefined,geocoding,Javascript,Google Maps Api 3,Global Variables,Undefined,Geocoding,有这么多关于这个话题的线索,但我仍然无法找到一个适合我的案例的答案 以下是我想做的: 1) 使用Google Maps API v3定义获取地理代码的函数 2) 使用相关地址调用函数,以获取此特定地址的地理代码 如果我将 alert(address); 之后 因为我需要它作为一个全局变量,不幸的是这不是一个选项。当尝试将'address'变量用作全局函数时,我总是得到'undefined'作为值 <!DOCTYPE html> <html> <head&
alert(address);
之后
因为我需要它作为一个全局变量,不幸的是这不是一个选项。当尝试将'address'变量用作全局函数时,我总是得到'undefined'作为值
<!DOCTYPE html>
<html>
<head>
<script src="http://maps.googleapis.com/maps/api/js?sensor=false"
type="text/javascript"></script>
</head>
<script type="text/javascript">
var geocoder;
var address;
address = 'Frankfurt, Germany';
function codeAddress() {
geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
address = results[0].geometry.location;
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
codeAddress(address);
alert(address);
</script>
</body>
</html>
var地理编码器;
var地址;
地址='德国法兰克福';
函数代码地址(){
geocoder=新的google.maps.geocoder();
geocoder.geocode({'address':address},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
地址=结果[0].geometry.location;
}否则{
警报(“地理编码因以下原因未成功:“+状态”);
}
});
}
代码地址;
警报(地址);
有人能帮我吗?盯着代码看,我似乎什么也做不到。
我感谢所有的帮助,谢谢 问题是
geocode
是异步的,不是吗
这意味着“在稍后的某个时间”调用地理代码回调,这是在首次调用原始post中的外部alter之后。(它不应该警告未定义,因为它首先被设置为一个特定的字符串,所以我将忽略该语句。)
见:
AJAX中的A代表异步。这意味着发送请求(或者更确切地说接收响应)将从正常执行流中删除。在您的示例中,$.ajax立即返回下一条语句return result;,在调用作为成功回调传递的函数之前执行
并专门针对上下文:
地理编码功能是异步的。这意味着发送请求(或者更确切地说接收响应)将从正常执行流中删除。在您的示例中,geocode立即返回,下一条语句alert(address)在调用作为成功回调传递的函数之前执行
谢谢你们的回答。我现在能够完成我的项目了!!!在下面找到我的最终代码。该代码填充地址数组的地理代码。玩得开心
<!DOCTYPE html>
<html>
<head>
<script src="http://maps.googleapis.com/maps/api/js?sensor=false"
type="text/javascript"></script>
</head>
<BODY>
<DIV ID="pro"></div>
<DIV ID="adr"></div>
<script type="text/javascript">
var geocoder;
var addresses = "Berlin, Deutschland; Frankfurt, Deutschland; Broadway Street, New York City, New York, USA";
geocoder = new google.maps.Geocoder();
var address = addresses.split(';');
var i = 0;
var timeout = 600;
codeAddress(i);
function codeAddress(i) {
geocoder.geocode( { 'address': address[i]}, function(results, status)
{
if (status == google.maps.GeocoderStatus.OK) {
document.getElementById("adr").innerHTML = document.getElementById("adr").innerHTML + "<BR>{location: new google.maps.LatLng" + results[0].geometry.location + ", weight: 1},";
} else {
if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT)
{
setTimeout(function() { addMarker(position); }, (timeout * 3));
}
}
i++;
var percent = ((i) / address.length) * 100;
percent = percent.toFixed(2);
percent = percent + " %";
document.getElementById("pro").innerHTML = percent;
if (i < address.length)
{
setTimeout(function() { codeAddress(i); }, (timeout));
}
});
}
</script>
</body>
</html>
var地理编码器;
var address=“德国柏林;德国法兰克福;美国纽约市百老汇街”;
geocoder=新的google.maps.geocoder();
var address=addresses.split(“;”);
var i=0;
var超时=600;
代码地址(i);
函数代码地址(i){
geocoder.geocode({'address':address[i]},函数(结果,状态)
{
if(status==google.maps.GeocoderStatus.OK){
document.getElementById(“adr”).innerHTML=document.getElementById(“adr”).innerHTML+“
{location:new google.maps.LatLng”+结果[0].geometry.location+”,权重:1},”;
}否则{
if(status==google.maps.GeocoderStatus.OVER\u QUERY\u LIMIT)
{
setTimeout(function(){addMarker(position);},(timeout*3));
}
}
i++;
变量百分比=((i)/地址长度)*100;
百分比=固定的百分比(2);
百分比=百分比+“%”;
document.getElementById(“pro”).innerHTML=百分比;
if(i<地址长度)
{
setTimeout(function(){codemaddress(i);},(timeout));
}
});
}
非常感谢您的回答,可能是重复的。这无疑让我向前迈出了至少两步。不幸的是,我仍然无法解决这个问题,因为我没有将地址输出到“doOtherStuffWithAddress”函数。@Kleni验证获得的数据是否符合预期-使用调试器代码>附加一个断点(在Firebug/Firefox和Chrome中运行良好,在IE中使用很烦人)。然后查看以确保结果[0].geometry.location
包含预期值,如果不包含,请找出原因:D如果它确实包含预期值,请在调用的相应函数中执行相同的检查(例如,doOther..
),并继续工作,直到遇到意外数据的确切位置。
function codeAddress() {
geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
// Only use address *after the callback success* and get rid of
// the global variable if possible - note how it's just passed here.
// You can use `debugger;` to stop here and check the value to
// make sure it is set as appropriate.
var address = results[0].geometry.location;
alert(address);
doOtherStuffWithAddress(address);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
function doOtherStuffWithAddress (address) {
// don't need no stinking globals
}
<!DOCTYPE html>
<html>
<head>
<script src="http://maps.googleapis.com/maps/api/js?sensor=false"
type="text/javascript"></script>
</head>
<BODY>
<DIV ID="pro"></div>
<DIV ID="adr"></div>
<script type="text/javascript">
var geocoder;
var addresses = "Berlin, Deutschland; Frankfurt, Deutschland; Broadway Street, New York City, New York, USA";
geocoder = new google.maps.Geocoder();
var address = addresses.split(';');
var i = 0;
var timeout = 600;
codeAddress(i);
function codeAddress(i) {
geocoder.geocode( { 'address': address[i]}, function(results, status)
{
if (status == google.maps.GeocoderStatus.OK) {
document.getElementById("adr").innerHTML = document.getElementById("adr").innerHTML + "<BR>{location: new google.maps.LatLng" + results[0].geometry.location + ", weight: 1},";
} else {
if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT)
{
setTimeout(function() { addMarker(position); }, (timeout * 3));
}
}
i++;
var percent = ((i) / address.length) * 100;
percent = percent.toFixed(2);
percent = percent + " %";
document.getElementById("pro").innerHTML = percent;
if (i < address.length)
{
setTimeout(function() { codeAddress(i); }, (timeout));
}
});
}
</script>
</body>
</html>