开发者

Google Maps API delay in loading variable

I am trying to use the Google Maps API to fetch the city name from a zipcode. This is not my strength (I'm more of a PHP person), so I am using sample code I found, with some modifications suggested by a friend.

The problem is, after I call the function my global variable with the city name is still at it's initialized value of null. If, however, I do an alert with this value, the rest of the processing suddenly has the correct value loaded! I tried putting in a time delay to see if Google was just slow in returning the value, but it makes no difference.

Here's the function:

var geocoder = new google.maps.Geocoder();
function getGoogleAddress(zipcode) {
    //var gcity = "N/A"; switch to using global var defined above
    geocoder.geocode( { 'address': zipcode}, function (result, status) {
        for (var component in result[0]['address_components']) {
            for (var i in result[0]['address_components'][component]['types']) {
                if (result[0]['address_components'][component]['types'][i] == "locality") {
                    gcity = result[0]['address_components'][component]['short_name'];
                    break;
                }
            }
        }
    });
}

And this is where it's called from...including the alert and the pause:

        gcity="";
        getGoogleAddress(form.zip.value);
  开发者_如何学Python      var holdcity = gcity;
        var date = new Date();
        var curDate = null;
        do { curDate = new Date(); }
        while(curDate-date < 2000);
        alert(gcity);

As I said, the alert returns null, but the rest of the processing has the proper city name in gcity. If I leave out the alert, the rest of the processing fails because gcity is null.

Any advice or suggestions are greatly appreciated. Thanks.


Asynchronous.

The function (result, status) { is only executed when Google's servers have responded. The rest of your getGoogleAddress function doesn't wait for that, but exits, and Javascript continues execution at var holdcity = gcity.

The reason it works after the alert, is that by then, Google will have responded, and the gcity variable will have been executed.

Possible solution:

var geocoder = new google.maps.Geocoder();
function getGoogleAddress(zipcode, successFunction) {
    //var gcity = "N/A"; switch to using global var defined above
    geocoder.geocode( { 'address': zipcode}, function (result, status) {
        for (var component in result[0]['address_components']) {
            for (var i in result[0]['address_components'][component]['types']) {
                if (result[0]['address_components'][component]['types'][i] == "locality") {
                    var gcity = result[0]['address_components'][component]['short_name'];
                    successFunction(gcity);
                    break;
                }
            }
        }
    });
}

And this is where it's called from...including the alert and the pause:

    getGoogleAddress(form.zip.value, function (holdcity) {
        var date = new Date();
        var curDate = null;
        do { curDate = new Date(); }
        while(curDate-date < 2000);
        alert(holdcity);
    });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜