GPGVM
GPGVM

Reputation: 5619

Javascript local and global variables losing scope in callback function

I have tried different variable scopes and none seem to work? My callback is getting a valid result but no matter the scope of the variable I assign it to I lose the value once the callback ends??

var geocoder;
var Lat;
var Long;

function codeAddress()
{


    var geocoder = new google.maps.Geocoder();

    var addy1......

    geocoder.geocode({ 'address': fullAddress }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK)
        {
            Lat = results[0].geometry.location.lat();
            Long = results[0].geometry.location.lng();

        }
        else
        {
            alert("Geocode was not successful for the following reason: " + status);
        }


    });
    alert(Lat);
    document.getElementById("Address_AddyLat").type.value = Lat;
    document.getElementById("Address_AddyLong").value = Long;
}

Thank for your input.

Upvotes: 0

Views: 1669

Answers (3)

Sohil Desai
Sohil Desai

Reputation: 2988

As Ameen said geocode is a asynch process so you need to put your alert & display code in callback function. and your another mistake is that you are using lat() & lng() as a method, its not a method its a property you just need to use it directly. so your code some look like.

geocoder.geocode({ 'address': fullAddress }, function (results, status) {
    if (status == google.maps.GeocoderStatus.OK)
    {
        Lat = results[0].geometry.location.lat;
        Long = results[0].geometry.location.lng;

        alert(Lat);
        document.getElementById("Address_AddyLat").value = Lat;
        document.getElementById("Address_AddyLong").value = Long;
    }
    else
    {
        alert("Geocode was not successful for the following reason: " + status);
    }
});

Upvotes: 0

james emanon
james emanon

Reputation: 11807

I think Ameen has the right of it. Your element reference must be incorrect.

Try this:

document.getElementById("Address_AddyLat").value = Lat;

or

document.getElementById("Address_AddyLat").setAttribute("value",Lat);

Upvotes: 0

Ameen
Ameen

Reputation: 2586

geocode is an asynchronous function, so when you call it, it immediately returns and the next lines are executed before the value of Lat is set. Think of it this way:

geocoder.geocode({ 'address': fullAddress }, /*...*/); // 1
alert(Lat); // 2
document.getElementById("Address_AddyLat").type.value = Lat; // 3
document.getElementById("Address_AddyLong").value = Long; // 4

What you want to do is to actually read the value of Lat in the callback itself:

geocoder.geocode({ 'address': fullAddress }, function (results, status) {
    if (status == google.maps.GeocoderStatus.OK)
    {
        Lat = results[0].geometry.location.lat();
        Long = results[0].geometry.location.lng();

        alert(Lat);
        document.getElementById("Address_AddyLat").type.value = Lat;
        document.getElementById("Address_AddyLong").value = Long;
    }
    else
    {
        alert("Geocode was not successful for the following reason: " + status);
    }


});

Upvotes: 1

Related Questions