Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jquery从另一个函数获取变量_Javascript_Jquery_Geolocation - Fatal编程技术网

Javascript jquery从另一个函数获取变量

Javascript jquery从另一个函数获取变量,javascript,jquery,geolocation,Javascript,Jquery,Geolocation,我有一个函数来获取一个当前地址(这个地址工作得很好),它是由另一个jquery函数触发的,我用它将结果通过ajax传递给mysql 脚本: var currentlocation; function getlocation(){ navigator.geolocation.getCurrentPosition( function( position ){ var lat = position.coords.latit

我有一个函数来获取一个当前地址(这个地址工作得很好),它是由另一个jquery函数触发的,我用它将结果通过ajax传递给mysql

脚本:

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);
    });
});
控制台打印出cl2和cl3的未定义结果,然后在一段时间后在cl1中更正google位置的结果

因此,我遇到的问题是获取变量currentlocation,以便稍后使用它,就像本例中的locationsql

由于geocode()函数本身是异步的,因此必须添加回调函数

function( results, status ) {
                                if ( status == google.maps.GeocoderStatus.OK && results[0] ) {
                                    var currentlocationTemp = results[0].formatted_address;
                                    callback(currentlocationTemp);
                                }
}
并在回调函数中接收变量以进行进一步操作

function callback(location){
      currentlocation = location;
}

好的,用jQuery延迟方法解决了这个问题

我的代码:

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

});
});

回调函数获取变量值,但我在将此值传递给locationsql时仍然遇到问题。是否尝试了我的建议?