问题描述
var geocoder,map,point,fmtAdd,marker;
函数mapLoad(){
geocoder = new google.maps.Geocoder();
var myOptions = {
zoom:15,
mapTypeControl:false,
mapTypeId:google.maps.MapTypeId.ROADMAP
}
map = new google .maps.Map(document.getElementById(map),myOptions);
address =W3 6BY;
if(address){geocoder.geocode({'address':address},geocodeResult);}
else {alert(Postcode Incorrect); window.close();}
}
function geocodeResult(results,status){
if(status =='OK'&& results.length> 0){
point = results [0 ] .geometry.location;
map.setCenter(point);
marker = new google.maps.Marker({map:map,position:point,draggable:true});
geocoder.geocode({latLng:point},function(results,status){
if(status =='OK'){
if(results.length == 0){
fmtAdd ='None';
} else {
fmtAdd = results [0] .formatted_address;
}
} else {
fmtAdd ='Error' ;
}
alert(fmtAdd); //显示地址
});
alert(fmtAdd); //说undefined;
} else {
alert(Error:+ status);
}
}
mapLoad();
我想显示英国用户输入的格式地址。但我不明白为什么第二个警报是未定义的?我没有在第一行定义变量fmtAdd吗?
您的第二个警报实际上是您的第一个警报因为它是首先执行的( geocode()
是非阻塞的 - 它会立即返回)。
此时您已经定义 fmtAdd
,但你没有初始化它。
var foo;警报(FOO);
警报未定义
。
回答评论:
这是正确的。一旦传递给 geocode()
的回调函数为它设置一个值,变量就会被初始化。而恰恰是这样。 在之后,您可以从您的全局变量中检索值,也可以从您的函数以外获取。
这里的问题是您试图从 fmtAddr
之前检索您的回调函数完成(或甚至被调用)之前的值。
这是因为 geocode()
是非阻塞的。这意味着它会立即返回,这就是为什么您将回调函数传递给 geocode()
。
发生了什么
引用了这部分代码:
geocoder.geocode({latLng:point},function(results,status){
if(status =='OK'){
if(results.length == 0){
fmtAdd ='None';
} else {
fmtAdd = results [0] .formatted_address;
}
} else {
fmtAdd ='Error' ;
}
alert(fmtAdd); //显示地址
});
alert(fmtAdd); //说undefined;
按时间顺序排列:
- 您可以调用
geocode()
,并将回调传递给它。 / code>启动一个到google服务器的异步请求并立即返回 -
alert(fmtAdd); //说明未定义;
- 异步请求完成并调用您的回调函数
- 您的回调函数集
fmtAddr
您需要做什么
以正确的顺序执行你的应用程序:
- 创建一个函数,用于处理格式化地址。
- 从你的回调中调用这个函数。也就是说,在设置
fmtAdd
之后(实际上最好通过格式化的地址直接作为参数,不用全局变量)
var geocoder, map, point, fmtAdd, marker;
function mapLoad() {
geocoder = new google.maps.Geocoder();
var myOptions = {
zoom: 15,
mapTypeControl: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map"), myOptions);
address="W3 6BY";
if(address){geocoder.geocode({'address':address}, geocodeResult);}
else{alert("Postcode Incorrect");window.close();}
}
function geocodeResult(results, status) {
if (status == 'OK' && results.length > 0) {
point=results[0].geometry.location;
map.setCenter(point);
marker = new google.maps.Marker({map: map, position: point, draggable: true});
geocoder.geocode({latLng:point},function(results, status){
if(status == 'OK') {
if(results.length == 0) {
fmtAdd = 'None';
} else {
fmtAdd = results[0].formatted_address;
}
} else {
fmtAdd = 'Error';
}
alert(fmtAdd); // shows the address
});
alert(fmtAdd); // says undefined;
} else {
alert("Error: " + status);
}
}
mapLoad();
I want to show the formatted address from user's input who are in the UK. But I don't understand why the second alert is undefined? Didn't I defined the variable "fmtAdd" at the first line?
Your "second" alert is actually your first alert since it is executed first (geocode()
is non blocking - it returns immediately).
At that point you "defined" fmtAdd
, but you didn't initialize it.
var foo; alert(foo);
alerts undefined
.
answering comment:
This is correct. The variable is initialized once the callback function passed to geocode()
sets a value to it. And exactly that happens. After that "event" you can retrieve the value from your global variable also outside of your function.
The problem here is that you're trying to retrieve the value from fmtAddr
before your callback function has completed (or is even called).
This is because geocode()
is non-blocking. This means that it returns immediately, this is why you pass a callback function to geocode()
.
What happens
referring to this part of the code:
geocoder.geocode({ latLng: point }, function (results, status) {
if (status == 'OK') {
if (results.length == 0) {
fmtAdd = 'None';
} else {
fmtAdd = results[0].formatted_address;
}
} else {
fmtAdd = 'Error';
}
alert(fmtAdd); // shows the address
});
alert(fmtAdd); // says undefined;
In chronological order:
- you call
geocode()
, passing a callback to it geocode()
starts an asynchronous request to the google servers and returns immediatelyalert(fmtAdd); // says undefined;
- the asynchronous request completes and calls your callback function
- your callback function sets
fmtAddr
what you need to do
execute your application in the correct order:
- Create a function which does whatever you want to do with the formatted address.
- Call this function from your callback. That is, after you set
fmtAdd
(actually better would be to pass the formatted address directly to this function as a parameter, without using global variables)
这篇关于为什么我的地理编码无法显示地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!