uniapp检测手机是否打开定位权限Vue3-直接复制粘贴

安卓示例:

苹果示例:

代码实现(vue3写法):

const checkGPS = ()=>{
        console.log('开始监听GPS状态');
        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)) {
                console.log('gps未开启');
  	    		uni.showModal({
  	    			title: '提示',
  	    			content: '请打开定位服务功能',
  	    			showCancel: false, // 不显示取消按钮
  	    			success() {
  	    				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服务页面
  	    			}
  	    		});
				return false
  	    	}else{
                console.log('gps已开启');
                uni.getLocation({
                  type: "gcj02", // 返回可以用于uni.openLocation的经纬度
                  success: async(res) => {
                    console.log('经纬度',res);
					try {
						// 自己的逻辑代码(调接口)
					} catch (err) {
						console.log('err',err);
					}	
                  },
                });
				return true
            }
  	    } else if (system.platform === 'ios') {
            var cllocationManger = plus.ios.import('CLLocationManager');
			var enable = cllocationManger.locationServicesEnabled();
			var status = cllocationManger.authorizationStatus();
			plus.ios.deleteObject(cllocationManger);
			if (enable && status != 2) {
				console.log('手机系统的定位已经打开');
	            uni.getLocation({
                  type: "gcj02", // 返回可以用于uni.openLocation的经纬度
                  success: async(res) => {
                    console.log('经纬度',res);
					try {
						// 自己的逻辑代码(调接口)
					} catch (err) {
						console.log('err',err);
					}
                  },
                });
				return true
			} 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);
					}
				});
				return false
			}
  	    }
    }

如果要换成vue2的写法:

const checkGPS = () => {} 改成 checkGPS(){}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值