// 定位开启状态 true=开启,false=未开启
let bool = false
// android平台
if (uni.getSystemInfoSync().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);
bool = mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)
}
// ios平台
if (uni.getSystemInfoSync().platform == 'ios') {
var cllocationManger = plus.ios.import("CLLocationManager");
var enable = cllocationManger.locationServicesEnabled();
var status = cllocationManger.authorizationStatus();
plus.ios.deleteObject(cllocationManger);
bool = enable && status != 2
}
// 未开启定位功能
if (bool === false) {
uni.showModal({
title: '提示',
content: '请打开定位服务',
success: ({
confirm,
cancel
}) => {
if (confirm) {
// android平台
if (uni.getSystemInfoSync().platform == 'android') {
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);
var main = plus.android.runtimeMainActivity();
main.startActivity(intent); // 打开系统设置GPS服务页面
}
// ios平台
if (uni.getSystemInfoSync().platform == 'ios') {
var UIApplication = plus.ios.import("UIApplication");
var application2 = UIApplication.sharedApplication();
var NSURL2 = plus.ios.import("NSURL");
var setting2 = NSURL2.URLWithString("App-Prefs:root=Privacy&path=LOCATION");
application2.openURL(setting2);
plus.ios.deleteObject(setting2);
plus.ios.deleteObject(NSURL2);
plus.ios.deleteObject(application2);
}
}
// 用户取消前往开启定位服务
if (cancel) {
// do sth...
}
}
});
}
网友评论