【问题标题】:google maps geocoding and order of initialization谷歌地图地理编码和初始化顺序
【发布时间】:2012-05-10 16:30:58
【问题描述】:

所以我最近遇到了一个问题,也许你们可以帮忙。

首先,我创建了网站和标记,并尝试检索中心点以对地址进行反向地理编码。

我的代码如下:

function ReverseGeocode(lat, lng)
{
    var latlng = new google.maps.LatLng(lat, lng);
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({"latLng": latlng}, function(results, status)
    {
        if (status != google.maps.GeocoderStatus.OK)
        {
            alert("Geocoding has failed due to "+ status);
        }
        address = results[0].formatted_address;
    });
}

我在这里遇到的问题是,当我尝试将“地址”传回时(地址现在是一个全局变量),我得到的只是“未定义”。

这是我试图将其传回的代码: sendString += '&lat=' + lat; sendString += '&lng=' + lon; ReverseGeocode(center.lat(), center.lng()); alert(""+地址);

sendString += '&address=' + address 
var currentLang = "en"
sendString += '&phone=' + document.getElementById("number").value;
sendString += '&email=' + document.getElementById("email").value;
sendString += ($("prefsms").checked)?'&contactMethod=sms':'&contactMethod=email';
sendString += '&serviceType=' + document.getElementById("serviceType").value;
sendString += '&language=' + currentLang;
alert(""+sendString);

在我的警告框中,我得到的只是“未定义”。然而,如果我在 ReverseGeocode 函数中添加另一个警报框,我将在警报框中获取地址,但这会发生在外部函数中的警报框之后。

关于发生了什么的任何想法?我原以为 ReverseGeocode 函数中的警报框会先出现,而不是相反。

谢谢!

【问题讨论】:

    标签: javascript google-maps geocoding


    【解决方案1】:

    正如 Heitor Chang 所说,地理编码是异步的 - 所以当您尝试返回地址时,它会返回给您作为回调传递给 geocoder.geocode() 的函数。使困惑?看到这个:

    function ReverseGeocode(lat, lng)
    {
        var latlng = new google.maps.LatLng(lat, lng);
        var geocoder = new google.maps.Geocoder();
        geocoder.geocode({"latLng": latlng}, function(results, status)
        {
            if (status != google.maps.GeocoderStatus.OK)
            {
                alert("Geocoding has failed due to "+ status);
            }
            return results[0].formatted_address; // this is how you might've been returning (i am just assuming since you didn't provide any code that returns address.
        });
    }
    

    现在你可以看到它返回到你传递给geocoder.geocode()的函数

    你应该做的是使用回调 - 你在这里传递了一个,可能没有意识到它 - 接受一个回调作为 ReverseGeocode 函数的第三个参数,当你得到结果为 OK 时,调用回调并返回地址。方法如下:

    function ReverseGeocode(lat, lng, cb)  // cb - callback, a function that takes the address as an argument.
    {
        var latlng = new google.maps.LatLng(lat, lng);
        var geocoder = new google.maps.Geocoder();
        geocoder.geocode({"latLng": latlng}, function(results, status)
        {
            if (status != google.maps.GeocoderStatus.OK)
            {
                alert("Geocoding has failed due to "+ status);
            }
            cb(results[0].formatted_address); // call the callback passing to it the address and we're done.
        });
    }
    

    如何使用它?这样:

    ReverseGeocode( LAT, LNG, function(address) {
        // do something with the address here. This will be called as soon as google returns the address.
    });
    

    【讨论】:

    • 你猜怎么着?到目前为止,我认为采用精心设计的方式很糟糕,只会让读者感到困惑——但你证明我错了!呵呵:)
    【解决方案2】:

    (反向)地理编码是异步的,这意味着请求发送到 Google 服务器,您的脚本继续运行,当 Google 发回其回复时,result is OK 块内的代码将执行。整体执行不一定会按照命令的编写顺序执行。

    要使用address 的值,您的代码必须包含在返回状态正常的代码块中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-29
      • 2011-08-28
      相关资源
      最近更新 更多