新增app、小程序检测用户是否开启GPS定位服务

master
chen 2022-04-18 18:13:41 +08:00
parent 81a2174045
commit efabcaf226
1 changed files with 79 additions and 0 deletions

View File

@ -37,6 +37,85 @@ const jsonp = function(url, data) {
})
}
const yayaMap = {
// 判断app、小程序是否开启GPS定位服务
checkOpenGPSServiceByAndroidIOS() {
// #ifdef APP-PLUS
let system = uni.getSystemInfoSync(); // 获取系统信息
if (system.platform === 'android') { // 判断平台
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: '请打开定位服务功能',
showCancel: false, // 不显示取消按钮
success() {
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); // 打开系统设置GPS服务页面
} else {
console.log('GPS功能已开启');
}
}
});
}
} else if (system.platform === 'ios') {
console.log("苹果");
var cllocationManger = plus.ios.import("CLLocationManager");
var enable = cllocationManger.locationServicesEnabled();
var status = cllocationManger.authorizationStatus();
plus.ios.deleteObject(cllocationManger);
if (enable && status != 2) {
console.log("手机系统的定位已经打开");
} else {
console.log("手机系统的定位没有打开");
uni.showModal({
title: '提示',
content: '请前往设置-隐私-定位服务打开定位服务功能',
showCancel: false, // 不显示取消按钮
success() {
var UIApplication = plus.ios.import("UIApplication");
var application2 = UIApplication.sharedApplication();
var NSURL2 = plus.ios.import("NSURL");
// var setting2 = NSURL2.URLWithString("prefs:root=LOCATION_SERVICES");
// var setting2 = NSURL2.URLWithString("App-Prefs:root=LOCATION_SERVICES");
var setting2 = NSURL2.URLWithString("app-settings:");
//var setting2 = NSURL2.URLWithString("App-Prefs:root=Privacy&path=LOCATION");
// var setting2 = NSURL2.URLWithString("App-Prefs:root=Privacy&path=LOCATION_SERVICES");
application2.openURL(setting2);
plus.ios.deleteObject(setting2);
plus.ios.deleteObject(NSURL2);
plus.ios.deleteObject(application2);
}
});
}
}
// #endif
// #ifdef MP-WEIXIN
wx.getSetting({
success: (res) => {
console.log(res,99);
if (!res.authSetting['scope.userLocation']) {
//打开提示框,提示前往设置页面
uni.showModal({
title:'为了更好的体验,请开启定位服务',
confirmText:'开启',
cancelText:'关闭',
success:(res)=> {
if(res.confirm) {
wx.openSetting({success (res) {}})
}
}
})
}
}
})
// #endif
},
// 平台判断
platformEv(){
// #ifdef APP-PLUS