为什么我的地理编码无法显示地址 [英] Why my geocode cannot show the address

查看:175
本文介绍了为什么我的地理编码无法显示地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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;

按时间顺序排列:


  1. 您可以调用 geocode(),并将回调传递给它。 / code>启动一个到google服务器的异步请求并立即返回

  2. alert(fmtAdd); //说明未定义;

  3. 异步请求完成并调用您的回调函数
  4. 您的回调函数集 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:

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 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:

  1. you call geocode(), passing a callback to it
  2. geocode() starts an asynchronous request to the google servers and returns immediately
  3. alert(fmtAdd); // says undefined;
  4. the asynchronous request completes and calls your callback function
  5. 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)

这篇关于为什么我的地理编码无法显示地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆