简体   繁体   English

Google Maps Geocoder无法分配结果

[英]Google Maps geocoder unable to assign result

Geocoder is working fine, but i can't seem to use result. Geocoder工作正常,但我似乎无法使用结果。

 var naddress;
 var ngc = new google.maps.Geocoder();
 ngc.geocode({'latLng': nll}, function(results, status) {
 if (status == google.maps.GeocoderStatus.OK) {
   if (results[0]) {
    alert(results[0].formatted_address);
    naddress = results[0].formatted_address;
    }
   }
 });
alert(naddress);

It show me the correct address at first alert(), but second is blank. 它在第一个alert()处向我显示正确的地址,但第二个为空白。 Am I doing something wrong? 难道我做错了什么?

There is no guarantee in which order the function passed to geocode and the first statement after the call to geocode will execute. 无法保证将函数传递给geocode顺序,并且无法执行对geocode的调用后的第一条语句。 So you either have to check to see if naddress has been assigned to yet, or put the code that needs it in the callback function. 因此,您必须检查naddress已经分配了naddress,或者将需要它的代码放入回调函数中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM