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

相关推荐
程序员码歌4 小时前
短思考第261天,浪费时间的十个低效行为,看看你中了几个?
前端·ai编程
Swift社区5 小时前
React Navigation 生命周期完整心智模型
前端·react.js·前端框架
若梦plus5 小时前
从微信公众号&小程序的SDK剖析JSBridge
前端
用泥种荷花6 小时前
Python环境安装
前端
Light606 小时前
性能提升 60%:前端性能优化终极指南
前端·性能优化·图片压缩·渲染优化·按需拆包·边缘缓存·ai 自动化
Jimmy6 小时前
年终总结 - 2025 故事集
前端·后端·程序员
烛阴6 小时前
C# 正则表达式(2):Regex 基础语法与常用 API 全解析
前端·正则表达式·c#
roman_日积跬步-终至千里6 小时前
【人工智能导论】02-搜索-高级搜索策略探索篇:从约束满足到博弈搜索
java·前端·人工智能
GIS之路6 小时前
GIS 数据转换:使用 GDAL 将 TXT 转换为 Shp 数据
前端
多看书少吃饭6 小时前
从Vue到Nuxt.js
前端·javascript·vue.js