如何自動打開GPS或當腳本運行函數獲取經度和緯度時請求權限打開GPS?如何在Android上自動打開GPS?
我運行在波動
及其自動開啓「GPS」在我的鉻,但是當我建立在我的Android智能手機上運行時,GPS不會出現。
我已經在配置中添加了「GeoLocation」插件,但仍然無法工作。
在此先感謝
如何自動打開GPS或當腳本運行函數獲取經度和緯度時請求權限打開GPS?如何在Android上自動打開GPS?
我運行在波動
及其自動開啓「GPS」在我的鉻,但是當我建立在我的Android智能手機上運行時,GPS不會出現。
我已經在配置中添加了「GeoLocation」插件,但仍然無法工作。
在此先感謝
應用程序無法在Android上自動打開GPS。最好的辦法是檢測GPS是否打開/關閉,然後打開Android位置設置頁面(如果是後者)。
您可以使用this plugin要做到這一點,例如:
cordova.plugins.diagnostic.isLocationEnabled(function(enabled){
console.log("Location is " + (enabled ? "enabled" : "disabled"));
if(!enabled){
cordova.plugins.diagnostic.switchToLocationSettings();
}
}, function(error){
console.error("The following error occurred: "+error);
});
需要注意的是,如果GPS關閉在Android上,地理位置API將返回超時錯誤,而不是拒絕的權限。如果在GPS關閉的情況下使用navigator.geolocation.watchPosition()
添加了位置監視,則GPS開啓後,您需要清除觀察者並在觀察者調用成功功能之前再次添加。
UPDATE
可以使用cordova-plugin-request-location-accuracy直接從應用程序內請求高精度定位模式(即GPS)。這將顯示一個本地確認對話框,如果用戶同意,GPS將自動啓用並要求用戶手動更改設置:
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);