从谷歌地图地理codeR v3的返回地址 [英] Return address from Google Maps Geocoder v3

查看:134
本文介绍了从谷歌地图地理codeR v3的返回地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下功能:

  $(函数(){
     。VAR地址= $(#地址)文本();
     VAR  -  [R;

     功能连接codeAddress(){
       地理codeR =新google.maps.Geo codeR();
       地理coder.geo code({地址:地址},功能(结果状态){
         如果(状态== google.maps.Geo coderStatus.OK){
           结果[0] .geometry.location
           R =结果[0] .geometry.location
           的console.log(里:+ R);
         } 其他 {
           警报(谷歌地图有一些很难找到+地址+状态);
         }
       });
     }

    EN codeAddress()
    的console.log(外:+ R);
});
 

我想返回地理codeD地址街景以及GMAP使用。里面返回正确的值,并underfined外面的回报。

所有的帮助是极大的AP preciated,我看过别的地方,这是不可能的返回值,这种方式却不得不检查与值得信赖的StackOverflow社区。

感谢你在前进。

解决方案

您可以这样来做,并调用地理code的回调函数中outputGeo功能。基本上,你正在访问R中的地理code回调之前,因此r是仍然不确定的:

  $(函数(){
     。VAR地址= $(地址)文本();
     VAR  -  [R;

     功能连接codeAddress(){
       地理codeR =新google.maps.Geo codeR();
       地理coder.geo code({地址:地址},功能(结果状态){
         如果(状态== google.maps.Geo coderStatus.OK){
           结果[0] .geometry.location;
           R =结果[0] .geometry.location;
           outputGeo(r)的;
         } 其他 {
           警报(谷歌地图有一些很难找到+地址+状态);
         }
       });
     }

    EN codeAddress();
    功能outputGeo(结果){
         的console.log(外部+结果);
    }
});
 


下面是服务如何地理编码工作的说明。

  

访问地理编码服务   异步的,因为谷歌地图   API需要调用一个   外部服务器。出于这个原因,就   需要传递一个回调方法   的完成后执行   请求。这个回调方法   处理结果(S)。需要注意的是   地理codeR可能返回多个   的结果。

I have the following function:

$(function() {
     var address = $("#address").text();
     var r;

     function encodeAddress() {
       geocoder = new google.maps.Geocoder();
       geocoder.geocode({'address' : address}, function(results, status) {
         if (status == google.maps.GeocoderStatus.OK) {
           results[0].geometry.location
           r = results[0].geometry.location
           console.log("inside:" + r);
         } else {
           alert("Google Maps had some trouble finding" + address + status);
         }
       });
     }

    encodeAddress()
    console.log("outside:" + r);
});

I am trying to return the geocoded address for use with Street View as well as Gmap. Inside returns the correct value and the outside returns underfined.

All help is greatly appreciated, I did read somewhere else that it is not possible to return the value this way but had to check with the trusty StackOverflow community.

Thank you in advance.

解决方案

You can do it this way, and call the outputGeo function within the Geocode's callback function. Basically, you are accessing r before the Geocode callback and thus r is still undefined:

$(function() {
     var address = $("address").text();
     var r;

     function encodeAddress() {
       geocoder = new google.maps.Geocoder();
       geocoder.geocode({'address' : address}, function(results, status) {
         if (status == google.maps.GeocoderStatus.OK) {
           results[0].geometry.location;
           r = results[0].geometry.location;
           outputGeo(r);
         } else {
           alert("Google Maps had some trouble finding" + address + status);
         }
       });
     }

    encodeAddress();
    function outputGeo(result){
         console.log("outside:" + result);
    }
});


Here is an explanation of how Geocoding service works.

Accessing the Geocoding service is asynchronous, since the Google Maps API needs to make a call to an external server. For that reason, you need to pass a callback method to execute upon completion of the request. This callback method processes the result(s). Note that the geocoder may return more than one result.

这篇关于从谷歌地图地理codeR v3的返回地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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