0

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

1
  • Did you try my suggestion ? Commented Sep 27, 2017 at 21:00

2 Answers 2

1

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

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

Comments

0

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

1 Comment

callback functions gets the variable value but still i have a problem on passing this value to locationsql.

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.