在使用uniapp獲取定位的時(shí)候 uni.getLocation() 第一次打開(kāi)手機(jī)的時(shí)候 會(huì)彈出 授權(quán)界面擦秽,但是有時(shí)候點(diǎn)錯(cuò)就不會(huì)再次彈出來(lái)了崎淳。今天做了個(gè)整理
創(chuàng)建 device.js文件
let system = uni.getSystemInfoSync(); // 獲取系統(tǒng)信息
/**檢查是否打開(kāi)GPS功能(android)**/
export const checkOpenGPSServiceByAndroid = () => {
if (system.platform === 'android') { // 判斷平臺(tái)
openGps()
}else if (system.platform == "ios") {
//蘋果打開(kāi)對(duì)應(yīng)的通知欄
uni.showModal({
title: "定位權(quán)限開(kāi)啟提醒",
content: "您還沒(méi)有開(kāi)啟定位權(quán)限趴酣,無(wú)法定位,請(qǐng)前往設(shè)置编矾!",
confirmText: "去設(shè)置",
success: function(res) {
if (res.confirm) {
var app = plus.ios.invoke("UIApplication", "sharedApplication");
var setting = plus.ios.invoke("NSURL", "URLWithString:", "app-settings:");
plus.ios.invoke(app, "openURL:", setting);
plus.ios.deleteObject(setting);
plus.ios.deleteObject(app);
}
}
});
}
}
/**
* 定位權(quán)限及彈出權(quán)限彈框 監(jiān)聽(tīng)用戶點(diǎn)擊按鈕
* **/
let num = 0;
export const openGps = () => {
plus.android.requestPermissions(
['android.permission.ACCESS_FINE_LOCATION'],
function(resultObj) {
console.log('resultObj---', resultObj);
var result = 0;
for (var i = 0; i < resultObj.granted.length; i++) {
var grantedPermission = resultObj.granted[i];
// console.log(6, '已獲取的權(quán)限:' + grantedPermission);
result = 1
}
for (var i = 0; i < resultObj.deniedPresent.length; i++) {
var deniedPresentPermission = resultObj.deniedPresent[i];
// console.log(5, '拒絕本次申請(qǐng)的權(quán)限:' + deniedPresentPermission);
num += 1
result = 0
}
for (var i = 0; i < resultObj.deniedAlways.length; i++) {
var deniedAlwaysPermission = resultObj.deniedAlways[i];
// console.log(4, '永久拒絕申請(qǐng)的權(quán)限:' + deniedAlwaysPermission);
num += 1
result = -1
}
// 若所需權(quán)限被拒絕,則打開(kāi)APP設(shè)置界面,可以在APP設(shè)置界面打開(kāi)相應(yīng)權(quán)限
if (result != 1) {
uni.showToast({
title: "請(qǐng)到系統(tǒng)設(shè)置打開(kāi)定位服務(wù)功能狼渊!",
icon: "none",
duration: 1000
})
setTimeout(() => {
var Intent = plus.android.importClass("android.content.Intent")
var Settings = plus.android.importClass("android.provider.Settings")
var Uri = plus.android.importClass("android.net.Uri")
var mainActivity = plus.android.runtimeMainActivity()
var intent = new Intent()
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
var uri = Uri.fromParts("package", mainActivity.getPackageName(), null)
intent.setData(uri)
mainActivity.startActivity(intent)
}, 1200)
}else {
//因?yàn)榘沧渴謾C(jī) 手機(jī)授權(quán)之后還需要打開(kāi)定位服務(wù)功能所有當(dāng)手機(jī)授權(quán)之后 調(diào)用下面的方法
var context = plus.android.importClass("android.content.Context")
var locationManager = plus.android.importClass("android.location.LocationManager")
var main = plus.android.runtimeMainActivity()
var mainSvr = main.getSystemService(context.LOCATION_SERVICE)
if (!mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)) {
uni.showModal({
title: '提示',
content: '請(qǐng)打開(kāi)定位服務(wù)功能',
success(res) {
if (res.confirm) {
if (!mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)) {
var Intent = plus.android.importClass('android.content.Intent');
var Settings = plus.android.importClass('android.provider.Settings')
var intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
main.startActivity(intent) // 打開(kāi)系統(tǒng)設(shè)置GPS服務(wù)頁(yè)面
} else {
}
}
}
});
return
}
}
},
function(error) {
console.log('申請(qǐng)權(quán)限錯(cuò)誤:' + error.code + " = " + error.message);
resolve({
code: error.code,
message: error.message
});
}
);
}
調(diào)用
import {checkOpenGPSServiceByAndroid } from '@/utils/device.js'
getGPS(){
uni.getLocation({
type: 'wgs84',
geocode:true,//設(shè)置該參數(shù)為true可直接獲取經(jīng)緯度及城市信息
success: function (res) {
this_.latitude = res.latitude
this_.longitude = res.longitude
},
fail: function (ress) {
//失敗調(diào)用
checkOpenGPSServiceByAndroid()
}
})
}
轉(zhuǎn)載:https://blog.csdn.net/weixin_42220533/article/details/128967939