查看: 731|回复: 0

uniapp开发小程序和app使用定位服务判断

[复制链接]

748

主题

39

回帖

3907

积分

管理员

总舵主

积分
3907
QQ
发表于 2024-4-2 12:48:20 | 显示全部楼层 |阅读模式
  1. if (uni.canIUse('getLocation')) {
  2.                                         console.log('可以用')
  3.                                        
  4.                                         // #ifdef APP
  5.                                         const appAuthorizeSetting = uni.getAppAuthorizeSetting();
  6.                                         // console.log(appAuthorizeSetting.locationAuthorized);
  7.                                         if (appAuthorizeSetting.locationAuthorized != 'authorized') {
  8.                                                 uni.showModal({
  9.                                                         title: '提示',
  10.                                                         content: '请开启手机位置服务并允许我们获取您的位置信息后重试',
  11.                                                         showCancel: false,
  12.                                                         success() {
  13.                                                                 uni.navigateBack();
  14.                                                         }
  15.                                                 })
  16.                                                 return;
  17.                                         }
  18.                                         // #endif
  19.                                        
  20.                                         // #ifdef MP-WEIXIN
  21.                                         uni.getSetting({
  22.                                                 success(res) {
  23.                                                         console.log(res)
  24.                                                         if (!res.authSetting['scope.userLocation']) {
  25.                                                                 uni.showModal({
  26.                                                                         title: '提示',
  27.                                                                         content: '位置获取失败,请在打开的设置页面在“位置消息”栏选择“使用小程序时允许”',
  28.                                                                         showCancel: false,
  29.                                                                         success() {
  30.                                                                                 uni.openSetting();
  31.                                                                         }
  32.                                                                 })
  33.                                                                 return;
  34.                                                         }
  35.                                                 }
  36.                                         });
  37.                                         // #endif
  38.                                        
  39.                                         uni.getLocation({
  40.                                                 success(res) {
  41.                                                         console.log(res)
  42.                                                         uni.setStorageSync('lat', res.latitude);
  43.                                                         uni.setStorageSync('lng', res.longitude);
  44.                                                         uni.navigateTo({
  45.                                                                 url: '/pages/user/userinfo'
  46.                                                         })
  47.                                                 }
  48.                                         })
  49.                                        
  50.                                 } else {
  51.                                         console.log('不可用')
  52.                                 }
复制代码


道不同,不相为谋!理想要有,万一实现了呢?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注公众号

相关侵权、举报、投诉及建议等,请发 E-mail:2513533699@qq.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.|鲁ICP备19000917号|鲁公网安备37078302000320号

在本版发帖
关注公众号
QQ客服返回顶部