0

I have defined variables named "delka" and "sirka", and I want to change their values in function below. Apparently, Im doing something wrong, because when the function ends, those variables arent affected by it. Why? Thx for answers.

var sirka;
var delka;
var mestoNaLL = document.getElementById("mesto").value;
var geocoder =  new google.maps.Geocoder();
        geocoder.geocode( { "address": mestoNaLL }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                sirka = results[0].geometry.location.lat();
                delka = results[0].geometry.location.lng();         
            } else {
                alert("Chyba: " + status);
            }
        });

        //undefined, why?
        alert(mestoNaLL + " " + sirka + " " + delka + " ");

EDIT

here is the same problem, right?

//works fine
alert(markers[index].title + " " + infoWindows[index].content);

                    markers[index].addListener("click", function() {

                        //error - undefined
                        alert(markers[index].title + " " + infoWindows[index].content);

                        infoWindows[index].open(map, markers[index]);
                        map.setZoom(14);
                        map.setCenter(markers[index].getPosition());            
                    });
Tom
  • 3
  • 3
  • Because the geocode method is doing asynchronous stuff : http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call/14220323#14220323 – Stranded Kid Dec 01 '15 at 14:17

1 Answers1

0

The below code is an asynchronous code. That means, it is not executed like everything others. Before that code is executed, your alert is executed, thereby giving you undefined.

geocoder.geocode( { "address": mestoNaLL }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        sirka = results[0].geometry.location.lat();
        delka = results[0].geometry.location.lng();         
    } else {
        alert("Chyba: " + status);
    }
});

The solution is to take the alert inside the OK:

geocoder.geocode( { "address": mestoNaLL }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        sirka = results[0].geometry.location.lat();
        delka = results[0].geometry.location.lng();         
        alert(mestoNaLL + " " + sirka + " " + delka + " ");
    } else {
        alert("Chyba: " + status);
    }
});
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • Thanks, but if I need the variables to take values from that function, because I have to work with them after? I dont really care about the alert, its just test message. – Tom Dec 01 '15 at 14:21
  • @Tom That is impossible, you need to change how your code works. You need to break up your code into parts. First part calls the geocode, second part is run when the response comes back. – epascarello Dec 01 '15 at 14:22
  • Asynchronous codes cannot behave that way @Tom. Sorry. – Praveen Kumar Purushothaman Dec 01 '15 at 14:27
  • 1
    Thanks all, I have rewritten that metod, it works now, but I have encountered my hopefully last problem: Im drawing markers to the google maps via their API, and the data for the markers are based on JSON reply. Everything works fine but when I register onlick event for the markes, it always opens the infoWindow for the last created marker, doesnt matter on which marker I actually click. U know I click on Washington, open info for Washignton, but when I click on anything else, lets say London, it open info for Washington again. Cant figure out why. – Tom Dec 01 '15 at 17:08