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

相关推荐
JELEE.35 分钟前
Django登录注册完整代码(图片、邮箱验证、加密)
前端·javascript·后端·python·django·bootstrap·jquery
TeleostNaCl3 小时前
解决 Chrome 无法访问网页但无痕模式下可以访问该网页 的问题
前端·网络·chrome·windows·经验分享
前端大卫4 小时前
为什么 React 中的 key 不能用索引?
前端
你的人类朋友4 小时前
【Node】手动归还主线程控制权:解决 Node.js 阻塞的一个思路
前端·后端·node.js
小李小李不讲道理6 小时前
「Ant Design 组件库探索」五:Tabs组件
前端·react.js·ant design
毕设十刻6 小时前
基于Vue的学分预警系统98k51(程序 + 源码 + 数据库 + 调试部署 + 开发环境配置),配套论文文档字数达万字以上,文末可获取,系统界面展示置于文末
前端·数据库·vue.js
2501_915909067 小时前
WebView 调试工具全解析,解决“看不见的移动端问题”
android·ios·小程序·https·uni-app·iphone·webview
mapbar_front7 小时前
在职场生存中如何做个不好惹的人
前端
牧杉-惊蛰7 小时前
纯flex布局来写瀑布流
前端·javascript·css
一袋米扛几楼988 小时前
【软件安全】什么是XSS(Cross-Site Scripting,跨站脚本)?
前端·安全·xss