Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.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
gelocation-异步回调不使用错误-Javascript_Javascript_Asynchronous_Geolocation_Asynccallback - Fatal编程技术网

gelocation-异步回调不使用错误-Javascript

gelocation-异步回调不使用错误-Javascript,javascript,asynchronous,geolocation,asynccallback,Javascript,Asynchronous,Geolocation,Asynccallback,myFn.getMyCurrentPosition(数据,函数(数据){//Async调用,需要像回调一样使用数据-此部分不起作用。如何使用当前位置坐标并在其他函数(如成功回调)中使用它 var myFn = { this.localVar : null, mysuccess : function (position) { this.myLocalVar = position.coords.latitude + ','+ position.coords.long

myFn.getMyCurrentPosition(数据,函数(数据){//Async调用,需要像回调一样使用数据-此部分不起作用。如何使用当前位置坐标并在其他函数(如成功回调)中使用它

var myFn = {
    this.localVar : null, 
    mysuccess : function (position) {
        this.myLocalVar = position.coords.latitude + ','+ position.coords.longitude; 
        return this.myLocalVar; 
    }, 
    myerror : function (error) {     
        return null; 
    }, 
    getMyCurrentPosition : function() {
        if(navigator && navigator.geolocation) { 
            //WORKS - mysuccess sets data asyncrhonously. 
            return navigator.geolocation.getCurrentPosition(this.mysuccess, this.myerror);          
        }
    }, 
    myInitializer : function(){
        //Initialize map, marker etc. for google maps API
        myFn.getMyCurrentPosition( function(){ //Async call, need to use data like a callback 
            //This code never runs! 
            if(this.myLocalVar){
                //doSomethingAfterCall - using this.myLocalVar, map, etc.;

            }
        });
    }
}
navigator.geolocation.getCurrentPosition(this.locationSuccess, this.locationError, {**timeout:1000**}); 
更新:尝试了以下操作:

var myFn = {
    mysuccess: function (position) {
        myFn.myInitializer(); 
    }, 
    myerror: function (error) { 
        myFn.myInitializer(); 
    }, 

     myInitializer : function(){
        //Initialize map, marker etc. for google maps API
    }, 
    onLoadSet : function(){
        navigator.geolocation.getCurrentPosition(this.mysuccess, this.myerror);
    }

} 

myFn.onLoadSet(); 
navigator.geolocation.getCurrentPosition(this.locationSuccess, this.locationError, {**timeout:1000**}); 

获取此错误:未能对“地理位置”执行“getCurrentPosition”:作为参数1提供的回调不是函数。

当调用
myFn.getMyCurrentPosition
时,向其传递两个参数(
数据和一个函数)

navigator.geolocation.getCurrentPosition(this.locationSuccess, this.locationError, {**timeout:1000**}); 
定义它时:

navigator.geolocation.getCurrentPosition(this.locationSuccess, this.locationError, {**timeout:1000**}); 
您没有告诉它接受任何参数(并且您也没有使用
arguments
对象)

navigator.geolocation.getCurrentPosition(this.locationSuccess, this.locationError, {**timeout:1000**}); 

如果您想对其执行任何操作,您需要实际使用传递给它的参数。

当您调用
myFn.getMyCurrentPosition
时,您将向它传递两个参数(
数据和一个函数)

navigator.geolocation.getCurrentPosition(this.locationSuccess, this.locationError, {**timeout:1000**}); 
定义它时:

navigator.geolocation.getCurrentPosition(this.locationSuccess, this.locationError, {**timeout:1000**}); 
您没有告诉它接受任何参数(并且您也没有使用
arguments
对象)

navigator.geolocation.getCurrentPosition(this.locationSuccess, this.locationError, {**timeout:1000**}); 

如果你想对它们做任何事情,你需要实际使用你传递给它的参数。

试试新的
navigator.geolocation.getCurrentPosition(this.locationSuccess, this.locationError, {**timeout:1000**}); 
我已经构建了一个扩展版本/polyfill,支持请求和查询地理位置的状态所有promisify:

navigator.geolocation.getCurrentPosition(this.locationSuccess, this.locationError, {**timeout:1000**}); 

Firefox也将很快支持撤销权限

试试新的
navigator.geolocation.getCurrentPosition(this.locationSuccess, this.locationError, {**timeout:1000**}); 
我已经构建了一个扩展版本/polyfill,支持请求和查询地理位置的状态所有promisify:

navigator.geolocation.getCurrentPosition(this.locationSuccess, this.locationError, {**timeout:1000**}); 

Firefox也将很快支持撤销权限。这就是有效的方法。我尝试了另一种方法,但可能弄乱了我的语法。有几个问题:
navigator.geolocation.getCurrentPosition(this.locationSuccess, this.locationError, {**timeout:1000**}); 
对于成功/错误回调中的myInitializer调用,JS需要引用变量myFn.method而不是this.method。由于locationSuccess/error是预期的回调,因此this.locationSuccess在getcurrentPosition的回调中起作用

var myFn = {
    locationSuccess: function (position) {
        myFn.myInitializer(position.coords); 
    }, 
    locationError: function (error) {  
        myFn.myInitializer();  
    }, 

     myInitializer : function(data){
        //Initialize map, marker etc. for google maps API
       //if(data) { doSomething(); }
       //else { doSomethingElse(); }

    }, 
    onLoadSet : function(){
        navigator.geolocation.getCurrentPosition(this.locationSucess, this.locationError, {timeout:1000});
    }

};

myFn.onLoadSet(); 
navigator.geolocation.getCurrentPosition(this.locationSuccess, this.locationError, {**timeout:1000**}); 
另一个问题是已知的:添加超时作为

navigator.geolocation.getCurrentPosition(this.locationSuccess, this.locationError, {**timeout:1000**}); 

这就是有效的方法。我尝试了另一种方法,但可能弄乱了我的语法。这是几个问题: 对于成功/错误回调中的myInitializer调用,JS需要引用变量myFn.method而不是this.method。由于locationSuccess/error是预期的回调,因此this.locationSuccess在getcurrentPosition的回调中起作用

var myFn = {
    locationSuccess: function (position) {
        myFn.myInitializer(position.coords); 
    }, 
    locationError: function (error) {  
        myFn.myInitializer();  
    }, 

     myInitializer : function(data){
        //Initialize map, marker etc. for google maps API
       //if(data) { doSomething(); }
       //else { doSomethingElse(); }

    }, 
    onLoadSet : function(){
        navigator.geolocation.getCurrentPosition(this.locationSucess, this.locationError, {timeout:1000});
    }

};

myFn.onLoadSet(); 
navigator.geolocation.getCurrentPosition(this.locationSuccess, this.locationError, {**timeout:1000**}); 
另一个问题是已知的:添加超时作为

navigator.geolocation.getCurrentPosition(this.locationSuccess, this.locationError, {**timeout:1000**}); 

getMyCurrentPosition
不接受任何参数…所以。
getMyCurrentPosition
不接受任何参数…所以。谢谢你的帮助。实际上我不需要传递任何东西,只需要使用返回的位置坐标。我还有一些局部变量,在调用getMyCurrentPosition的地方,所以我想使用它们和位置坐标。有没有办法做到这一点。因此,基本上,我只需要在设置位置/数据后进行回调,即获取返回的数据,而不传递任何内容。?var map=something;myFn.getMyCurrentPosition(函数(数据){//data需要是位置-lat/long…});没有返回的坐标。这是一个异步函数。你不能从异步函数返回。这就是我们传递回调的原因。你能告诉我如何传递/使用回调吗,这是我正在努力处理的部分。我在获得位置后有一系列功能,但只有在成功返回位置时才需要运行。所以我在Suces回调中,我必须移动我在Suces上尝试执行的任何操作?我不能真正按照我的方式写出它?让我试试。谢谢你的帮助。实际上我不需要传递任何东西,只需要使用返回的位置坐标。我还有一些局部变量,在调用getMyCurrentPosition的地方,所以我想使用它们d位置坐标。有没有办法做到这一点。因此,基本上,我只需要在设置位置/数据后进行回调,即获取返回的数据,而不传递任何内容。?var map=something;myFn.getMyCurrentPosition(函数(数据){//data需要是位置-lat/long…});没有返回的坐标。这是一个异步函数。你不能从异步函数返回。这就是我们传递回调的原因。你能告诉我如何传递/使用回调吗,这是我正在努力处理的部分。我在获得位置后有一系列功能,但只有在成功返回位置时才需要运行。所以我在Success回调中,我必须移动我在Success上尝试做的任何事情?我不能用我现在的方式写出来?让我试试。