当前位置: 首页 > news >正文

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


http://www.mrgr.cn/news/12764.html

相关文章:

  • linux 工作中常用的命令
  • IOS 16 封装网络请求和log打印
  • 4. shell 自定义变量
  • 畅捷通CRM newleadset.php SQL注入漏洞复现
  • UDS 网络层PDU 类型
  • 开学季有哪些必备的好物?学生家长请收下这份清单
  • git rebase和git merge的区别
  • DRF——pagination分页模块
  • QL5010-16-ASEMI逆变焊机专用整流桥QL5010
  • 【Java】数据类型与变量(一)
  • 山东大数据职称考试复习
  • Getting RateLimitError while implementing openai GPT with Python
  • Android 12系统源码_输入系统(二)InputManagerService服务架构
  • input中文输入频繁触发input方法问题
  • Open3D编译安装
  • 前端数据爬虫之puppeteer
  • 如何实现对象克隆
  • 深入理解Vue的beforeDestroy钩子函数及其实际应用
  • 通过历史进行数据预测的算法
  • layui2.9 树组件默认无法修改节点图标,修改过程记录下