Nita
Nita

Reputation: 561

jquery getting variable from another function

I have function to get a current address(that works well) which is triggered by another jquery function that i use to pass results with ajax to mysql.

script:

var currentlocation;

function getlocation(){

        navigator.geolocation.getCurrentPosition(
            function( position ){ 

                var lat = position.coords.latitude;
                var lng = position.coords.longitude;
                var google_map_pos = new google.maps.LatLng( lat, lng );

                var google_maps_geocoder = new google.maps.Geocoder();
                google_maps_geocoder.geocode(
                    { 'latLng': google_map_pos },
                    function( results, status ) {
                        if ( status == google.maps.GeocoderStatus.OK && results[0] ) {
                            console.log( results[0].formatted_address );
                            currentlocation = results[0].formatted_address;
                            console.log('cl1: ' + currentlocation);
                        }
                    }
                );
            },
            function(){ 
            }
        );
}

//and jquery function (there are multiple functions similar to this one    using the getlocation()

$(document).ready(function () {
    $('[id^=start-]').on('click', function (e) {
        getlocation();
        console.log('cl2: ' + currentlocation);
        var locationsql = currentlocation;
        console.log('cl3:' + locationsql);
    });
});

console prints out undefined results for cl2 and cl3, then after a while correct result of google location in cl1.

So what I have a problem with is getting the variable currentlocation so I can use it later as in this case locationsql

Advice highly apriciated

Upvotes: 0

Views: 447

Answers (2)

Nita
Nita

Reputation: 561

Well, worked it out with jQuery Deferred Method.

My code:

var currentlocationTemp;

function getlocation(){

        // set dfd variable
        var dfd = $.Deferred();

        /* Chrome need SSL! */
        var is_chrome = /chrom(e|ium)/.test( navigator.userAgent.toLowerCase() );
        var is_ssl    = 'https:' == document.location.protocol;
        if( is_chrome && ! is_ssl ){
            return false;
        }
        navigator.geolocation.getCurrentPosition(
            function( position ){ // success cb
                /* Current Coordinate */
                var lat = position.coords.latitude;
                var lng = position.coords.longitude;
                var google_map_pos = new google.maps.LatLng( lat, lng );

                /* Use Geocoder to get address */
                var google_maps_geocoder = new google.maps.Geocoder();
                google_maps_geocoder.geocode(
                    { 'latLng': google_map_pos },
                    function( results, status ) {
                        if ( status == google.maps.GeocoderStatus.OK && results[0] ) {
                            console.log( results[0].formatted_address );
                            currentlocationTemp = results[0].formatted_address;
                            // resolve dfd variable                             
                            dfd.resolve();
                        }
                    }
                );

            },
            function(){ // fail cb
            }

        );
        // promise dfd variable
        return dfd.promise();
   }


$(document).ready(function () {
$('[id^=start-]').on('click', function (e) {

    function start() {
        currentlocation = currentlocationTemp;
        console.log('cl: ' + currentlocation); 
        }

    //// Que of the functions
    getlocation().then(start);  

});
});

Upvotes: 1

Vino
Vino

Reputation: 3037

As geocode() function itself is asynchronous, you will have to add a callback function,

function( results, status ) {
                                if ( status == google.maps.GeocoderStatus.OK && results[0] ) {
                                    var currentlocationTemp = results[0].formatted_address;
                                    callback(currentlocationTemp);
                                }
}

And in callback function receive your variable for further manipulations

function callback(location){
      currentlocation = location;
}

Upvotes: 0

Related Questions