2016-07-15 230 views
0

當我的應用程序開始我檢查,看看當地的GPS功能時,若沒有它會彈出一個問題,使,然後將用戶定向到它們的設置頁面,在那裏他們必須手動啓用它:如何在應用程序啓動時「自動」啓用GPS?

cordova.plugins.diagnostic.switchToLocationSettings();然而,我看到其他應用程序,當他們啓動時,他們問這個問題,當用戶選擇'是'該應用程序只需自動啓用用戶GPS,而不必將他們帶到他們的設置頁面。

如何複製此「自動啓用」行爲?

回答

0

可以使用的cordova.plugins.diagnostic伴侶插件做到這一點(在Android) - cordova-plugin-request-location-accuracy

添加:

cordova plugin add cordova-plugin-request-location-accuracy 

然後使用它:

function onRequestSuccess(success){ 
    console.log("Successfully requested accuracy: "+success.message); 
} 

function onRequestFailure(error){ 
    console.error("Accuracy request failed: error code="+error.code+"; error message="+error.message); 
    if(error.code !== cordova.plugins.locationAccuracy.ERROR_USER_DISAGREED){ 
     if(window.confirm("Failed to automatically set Location Mode to 'High Accuracy'. Would you like to switch to the Location Settings page and do this manually?")){ 
      cordova.plugins.diagnostic.switchToLocationSettings(); 
     } 
    } 
} 

cordova.plugins.locationAccuracy.request(onRequestSuccess, onRequestFailure, cordova.plugins.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY); 
+0

感謝。很高興知道我可以做到這一點。不過,我安裝了這個軟件包,但是它在編譯時導致我的應用程序失敗。我發佈了一個新的問題到插件github,問題#1。 – rolinger

+0

另外,我剛剛發現了第一個音符...(在Android上)。有沒有類似的方法插件可以在iOS上使用? – rolinger

+0

對於編譯錯誤,請確保您已爲編譯過程編譯了Google Play服務附加程序。 – user126440

相關問題