简体   繁体   English

地理位置-异步回调无法处理错误-Javascript

[英]gelocation - asynchronous callback not working with error - Javascript

myFn.getMyCurrentPosition(data, function(data){ //Async call, need to use data like a callback - this section doesnt work. How can I use the current position coords and use it in a different function like a successfull callback? myFn.getMyCurrentPosition(data,function(data){//异步调用,需要像回调一样使用数据-本节不起作用,如何使用当前位置坐标并将其用于其他函数(如成功回调)中?

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

            }
        });
    }
}

Update: TRIED THIS: 更新:尝试此:

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

Get this error: Failed to execute 'getCurrentPosition' on 'Geolocation': The callback provided as parameter 1 is not a function. 得到此错误:无法在“地理位置”上执行“ getCurrentPosition”:作为参数1提供的回调不是函数。

When you call myFn.getMyCurrentPosition , you pass it two arguments ( data and a function). 调用myFn.getMyCurrentPosition ,将向其传递两个参数( data和函数)。

When you defined it: 定义时:

 function() { 

You didn't tell it to accept any arguments (and you didn't use the arguments object either). 您没有告诉它接受任何参数(也没有使用arguments对象)。

You need to actually use the arguments you pass to it if you want anything to be done with them. 如果您想对它们进行任何处理,则需要实际使用传递给它的参数。

Try something like the new Permission api 尝试类似新的Permission API的方法
I have built a extended version/polyfill of it with support for requesting & querying the state of the geolocation all promisify: browser-su 我已经构建了它的扩展版本/ polyfill,并支持请求和查询地理位置的状态,这一切都应运而生: browser-su

/***************
 *    query    *
 ***************/
su.query({
    name: 'geolocation'
}).then(permission => {
    console.log(permission) // {state, onchange}
    console.log(permission.state) // granted, prompt or denied
})


/***************
 *   request   *
 ***************/
su.request({
    name: 'geolocation',
    timeout: 5000 // Optional
}).then(position => {
    console.log(position)
} err => {
    console.log(err.name, err.message)
    // RequestDeniedError       User blocked access to geolocation
    // RequestDismissedError    User dismissed access to geolocation
    // RequestTimeoutError      Timeout expired
    // RequestUnavailableError  Position is unavailable
    // RequestUnsupportedError  This client dose not seem to have geolocation support
})

Firefox will also soon have support for revoking permission Firefox也将很快支持撤销权限

This is what worked. 这是有效的。 I tried the other approach but probably messed up something in my syntax. 我尝试了另一种方法,但是可能在语法上弄乱了某些内容。 It was a couple of issues: The JS needs to reference variable myFn.method instead of this.method for the myInitializer call within success/error callbacks. 这有两个问题:JS需要在成功/错误回调中为myInitializer调用引用变量myFn.method而不是this.method。 Because locationSuccess/error are expected callbacks, this.locationSucess works in the callback for getcurrentPosition. 因为locationSuccess / error是预期的回调,所以this.locationSucess在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(); 

Another issue is known: Add a timeout as third parameter for the 另一个问题是已知的:将超时作为第三个参数添加到

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM