0

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.

3
  • 2
    Looks like a dup of stackoverflow.com/questions/14220321/… Commented Sep 17, 2013 at 4:05
  • 2
    They're not losing scope, they simple were not assigned a value when you accessed them. geocode is an asynchronous function, and you will need to put everything that works with its result in the callback Commented Sep 17, 2013 at 4:08
  • Well I tried that originally....and switched to this. I can try again. if (status == google.maps.GeocoderStatus.OK) { document.getElementById("Address_AddyLat").value = results[0].geometry.location.lat(); document.getElementById("Address_AddyLong").value = results[0].geometry.location.lng(); } doesn't update the form??? Commented Sep 17, 2013 at 4:11

3 Answers 3

1

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);
    }


});
Sign up to request clarification or add additional context in comments.

2 Comments

Whoever down voted could rub my nose with a correct answer. Ameen it does NOT work. alert(Lat) has proper value but the document elements do not update??
Well, what's the type of Address_AddyLat? Is it an input with type text? If so, you want to do document.getElementById("Address_AddyLat").value = Lat (without the type)
0

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);

Comments

0

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);
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.