2017-10-15 57 views
1

我在Ionic 3中編寫了一個函數來獲取用戶的位置並使用用戶的經度和緯度獲取其地址。該功能還會檢查用戶的位置設置是否啓用,如果不啓用,則會調用this.diagnostic.switchToLocationSettings()它基本顯示用戶打開的位置設置。我還添加了this.diagnostic.registerLocationStateChangeHandler方法來檢查用戶是否實際開啓了我之前顯示的位置設置中的位置。如果一切正常,我會繼續運行其他流程,例如獲取地理位置並翻譯成地址。Ionic 3無法在回調函數中拒絕承諾

下面是我的完整代碼:

getMyLocation() { 
    this.geoOptions = { 
     enableHighAccuracy: false 
    } 
    const loadingMsg = this.loadingCtrl.create({ 
     content: "Getting your location..." 
    }) 

    loadingMsg.present(); 



    this.diagnostic.isLocationEnabled().then(test => { 
     this.diagnostic.registerLocationStateChangeHandler((state)=>{ 
     if((this.platform.is('ios')) && (state !== this.diagnostic.permissionStatus.GRANTED || 
      state !== this.diagnostic.permissionStatus.GRANTED_WHEN_IN_USE) || 
      (this.platform.is('android')) && (state === this.diagnostic.locationMode.LOCATION_OFF)){ 
      return Promise.reject("Please switch on your location"); 
      } 
     }) 
     if (!test) { 
     this.diagnostic.switchToLocationSettings() 
     } 

     // return this.geolocation.getCurrentPosition(this.geoOptions) 
     return this.geolocation.getCurrentPosition(this.geoOptions) 
     // console.log("Error! Please switch on your location"); 
     // return Promise.reject("Error! Please switch on your location"); 
    },(err:any)=>{ 
     loadingMsg.dismiss(); 
     console.log("error : " + err); 

    }).then((pos: Geoposition) =>{ 

     return this.nativeGeocoder.reverseGeocode(pos.coords.latitude, pos.coords.longitude); 

    },(err: PositionError)=>{ 
     loadingMsg.dismiss(); 
     console.log("error : " + err.message); 
     return Promise.reject(err.message); 

    }).then((result: NativeGeocoderReverseResult) =>{ 

     this.currentAddress = result.locality+", "+result.countryName; 
     loadingMsg.dismiss(); 

    },(err:any)=>{ 

     console.log("error : " + err); 
     loadingMsg.dismiss(); 
     return Promise.reject(err); 

    }) 
    } 

的問題是我無法停止的承諾鏈接如果this.diagnostic.switchToLocationSettings()被觸發後,用戶沒有開啓。我試圖停止鏈接的地方在this.diagnostic.registerLocationStateChangeHandler回調函數內。我是一個承諾鏈接方法的初學者。

回答

0

你可以這樣說:

您可以在您需要abort promise chain的地方使用throw new Error('abort promise chain');return null;

this.diagnostic.isLocationEnabled().then(test => { 
     this.diagnostic.registerLocationStateChangeHandler((state)=>{ 
     if((this.platform.is('ios')) && (state !== this.diagnostic.permissionStatus.GRANTED || 
      state !== this.diagnostic.permissionStatus.GRANTED_WHEN_IN_USE) || 
      (this.platform.is('android')) && (state === this.diagnostic.locationMode.LOCATION_OFF)){ 
      throw new Error('abort promise chain');//here 
      return null; 
      } 
     }) 
+1

謝謝!我設法得到它現在的工作,雖然有幾個問題我在我的代碼中發現的...所以爲了簡單起見,我刪除'this.diagnostic.registerLocationStateChangeHandler',我接着說:拋出新的錯誤( '放棄諾言鏈');'進入'if(!test)'語句。然後我發現,如果我的承諾鏈中有錯誤,我不必爲所有'then(success,err)'創建錯誤處理程序,而只是在最後一個鏈上創建它或將'.catch( err)'。 –

+1

好極了!在這裏你可以找到更多的選擇:https://github.com/petkaantonov/bluebird/issues/581 – Sampath

+0

我看到有更多的方式來處理使用藍鳥的承諾,謝謝! –