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(){}

相关推荐
kyriewen1 小时前
百度用6%成本碾压硅谷?中国AI把性价比玩明白了
前端·百度·ai编程
kyriewen2 小时前
你还在手动敲命令部署?GitHub Actions 让你 push 即上线,摸鱼时间翻倍
前端·面试·github
DK185838322522 小时前
知识付费会员小程序/付费圈子系统——课程兑换码+会员体系完整实战,开源运营级方案
小程序·uni-app·开源·php
Csvn3 小时前
Pinia 状态管理
前端
不减20斤不改头像3 小时前
手机一句话开发贪吃蛇!TRAE SOLO 移动端 AI 编程实测
前端·后端
xuankuxiaoyao4 小时前
Vue.js实践-组件基础下
前端·javascript·vue.js
小白学大数据4 小时前
JS 混淆加密下的 Python 爬虫解决方案
javascript·爬虫·python
一棵白菜4 小时前
Claude Code + Amazon Bedrock 使用指南
前端
大家的林语冰5 小时前
前端周刊:axios 疑遭朝鲜黑客“钓鱼“;CSS 新函数上线;npm 上线深色主题;Oxlint 兼容表;ESLint 支持 Temporal......
前端·javascript·css
哀木6 小时前
一个简单的套壳方案,就能让你的 Agent 少做重复初始化
前端