jubl
jubl

Reputation: 1

How do I pass a variable from Google maps geocoder to another function?

How could I get the value from test2 variable passed on to another function? I've been trying with global variables and by chaining functions, as suggested in another thread I found, but couldn't get those to work.

function codeAddress(addresstocode) {

var test2 = ['123','321'];

geocoder.geocode( { 'address': addresstocode}, function(results, status) {

    if(status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);

        coded_lat = results[0].geometry.location.Ka;
        coded_long = results[0].geometry.location.La;

        //coded_location = [{"latitude" : coded_lat, "longitude" : coded_long}];

        //test2 = returnAddress(coded_location);

        test2 = [coded_lat,coded_long];

        //coded_lat = coded_location[0].latitude;
        //coded_long = coded_location[0].longitude;
        //returnAddress(coded_location);
    }

});

console.log('test2: '+test2);

return test2;


}

Upvotes: 0

Views: 586

Answers (2)

Trevor Miles Wagner
Trevor Miles Wagner

Reputation: 295

You should also never use the letter designations of location.Ka, location.La. The "Ka" and "La" change often. Use the appropriate functions of location.lat() or location.lng(). That way your code doesn't break all of a sudden.

Upvotes: 1

ChristopheCVB
ChristopheCVB

Reputation: 7315

You have to create a function that receives the values like :

function geocodeOK(lat, lng){
    // Do what you want with lat lng
}

And in your code :

function codeAddress(addresstocode) {

    var test2 = ['123', '321'];

    geocoder.geocode({
        'address': addresstocode
    }, function(results, status) {

        if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);

            coded_lat = results[0].geometry.location.Ka;
            coded_long = results[0].geometry.location.La;
            geocodeOK(coded_lat, coded_long);
        }

    });

}

Upvotes: 1

Related Questions