为什么我的地理编码无法显示地址 [英] Why my geocode cannot show the address
问题描述
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);
警报未定义
。
回答评论:
我以为是一个全局变量,一旦地理编码赋予
一个值,我甚至可以从地理编码
函数中检索该值。
这是正确的。一旦传递给 这里的问题是您试图从 引用了这部分代码: 按时间顺序排列: 以正确的顺序执行你的应用程序: 之后(实际上最好通过格式化的地址直接作为参数,不用全局变量) 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 ( alerts answering comment: I thought it was a global variable, and once the geocode give
an value to it, I can retrieve that value even out of the geocode
function. This is correct. The variable is initialized once the callback function passed to The problem here is that you're trying to retrieve the value from referring to this part of the code: In chronological order:
execute your application in the correct order: (actually better would be to pass the formatted address directly to this function as a parameter, without using global variables) 这篇关于为什么我的地理编码无法显示地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! 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();
geocode()
is non blocking - it returns immediately).
At that point you "defined" fmtAdd
, but you didn't initialize it. var foo; alert(foo);
undefined
.
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.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
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;
geocode()
, passing a callback to itgeocode()
starts an asynchronous request to the google servers and returns immediatelyalert(fmtAdd); // says undefined;
fmtAddr
what you need to do
fmtAdd