当前位置:   article > 正文

微信小程序 引导地址授权 获取位置信息 uniapp_uni-app微信小程序 打开定位权限

uni-app微信小程序 打开定位权限

 概述

 获取位置信息,需要保证是否授权位置信息,有几个条件是导致无法授权的原因

(1)微信应用未授权定位设置

(2)首次进入小程序未授权位置信息

(3)小程序之前阻止过授权位置信息

(4)未打开手机定位设置

  1. onLoad(){
  2. this.getAddressInfo();
  3. },
  4. methods:{
  5. //地址信息获取
  6. getAddressInfo(){
  7. let that=this;
  8. try{
  9. uni.getLocation({
  10. type: 'gcj02',
  11. geocode:true,//设置该参数为true可直接获取经纬度
  12. success: function (resF){
  13. console.log(resF)
  14. },
  15. fail: function (){
  16. //地址获取失败提示用户执行相关操作
  17. that.openSetting();
  18. }
  19. });
  20. }catch(v){
  21. }
  22. }
  23. }

代码说明

uni.getLocation({

    type: 'gcj02',  

    geocode:true,//设置该参数为true可直接获取经纬度

    success: function (resF){

        console.log(resF)

    },

    fail: function (){

        //地址获取失败提示用户执行相关操作

        that.openSetting();

    }

});

uni.getLocation()会触发,小程序的位置授权

正常流程在以上四个条件都满足情况下,点击确定位置授权

就会进入success函数中,获取经纬度

否则就会进入fail函数中,进行位置授权引导

  1. openSetting(){
  2. const that = this;
  3. uni.getSetting({
  4. success: (res) => {
  5. if (res.authSetting['scope.userLocation'] == true) {
  6. uni.showModal({
  7. title: '提示',
  8. content: '请打开定位权限',
  9. success: function(res) {
  10. if (res.confirm) {
  11. that.getAddressInfo();
  12. } else if (res.cancel) {
  13. console.log('用户点击取消');
  14. }
  15. }
  16. });
  17. } else {
  18. uni.showModal({
  19. title: '打开定位权限',
  20. content: '是否开启授权',
  21. success: res => {
  22. if (res.confirm) {
  23. uni.openSetting({
  24. success: (res) => {
  25. let authSettings = res.authSetting
  26. if (authSettings['scope.userLocation'] == true) {
  27. that.getPageList();
  28. } else {
  29. uni.showModal({
  30. title: '提示',
  31. content: '打开定位权限,自动匹配所在城市',
  32. success: function(res) {
  33. if (res.confirm) {
  34. that.getAddressInfo();
  35. } else if (res.cancel) {
  36. console.log('用户点击取消');
  37. }
  38. }
  39. });
  40. }
  41. }
  42. })
  43. } else {
  44. uni.showToast({
  45. title: '你未开通地理位置授权',
  46. icon: 'none'
  47. })
  48. }
  49. },
  50. })
  51. }
  52. }
  53. })
  54. },

 执行以上代码,即可完成用户引导,直接使用即可

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/889375
推荐阅读
相关标签
  

闽ICP备14008679号