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

安卓示例:

苹果示例:

代码实现(vue3写法):

javascript 复制代码
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(){}

相关推荐
whuhewei1 小时前
为什么客户端不存在跨域问题
前端·安全
妮妮喔妮1 小时前
supabase的webhook报错
开发语言·前端·javascript
qq_12084093712 小时前
Three.js 大场景分块加载实战:从全量渲染到可视集调度
开发语言·javascript·数码相机
yivifu2 小时前
手搓HTML双行夹批效果
前端·html·html双行夹注
奔跑的卡卡3 小时前
Web开发与AI融合-第一篇:Web开发与AI融合的时代序幕
前端·人工智能
IT_陈寒3 小时前
Redis批量删除的大坑,差点让我加班到天亮
前端·人工智能·后端
帆张芳显3 小时前
智表ZCELL产品V3.6 版发布,新增系统预置右键菜单操作、页签栏操作等功能
前端·canva可画·excel插件
漂流瓶jz3 小时前
运行时vs编译时:CSS in JS四种主流方案介绍和对比
前端·javascript·css
Asmewill3 小时前
uv包管理命令
前端
发现一只大呆瓜3 小时前
深入浅出 Tree Shaking:Rollup 是如何“摇”掉死代码的?
前端·性能优化·vite