国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 熱點 > 微信 > 正文

微信小程序出現(xiàn)wx.getLocation再次授權(quán)問題的解決方法分析

2024-07-22 01:19:13
字體:
供稿:網(wǎng)友

本文實例講述了微信小程序出現(xiàn)wx.getLocation再次授權(quán)問題的解決方法。分享給大家供大家參考,具體如下:

首先,在page外定義一個公共函數(shù)用于發(fā)送獲取位置的請求

var getLocation = function (that) { wx.getLocation({  type: 'wgs84',  success: function (res) {   // 經(jīng)緯度   var latitude = res.latitude   var longitude = res.longitude   var aK = that.data.aK   wx.request({    url: 'https://api.map.baidu.com/geocoder/v2/?ak=' + aK + '&location=' + latitude + ',' + longitude + '&output=json',    data: {},    header: {     'content-type': 'application/json'    },    success: function (res) {     var city = res.data.result.addressComponent.city;     that.setData({      currentCity: city     })     wx.request({      url: 'xxx' + city,      data: {},      header: {       'content-type': 'application/json'      },      success: function (res) {       that.setData({        county: res.data,       })      },     })    }   })  },  fail: function () {   wx.showToast({    title: '授權(quán)失敗',    icon: 'success',    duration: 1000   })  } })}

然后,在page中需要位置調(diào)用page外部的getLocation 函數(shù)

wx.getSetting({    success: (res) => {     if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {//非初始化進入該頁面,且未授權(quán)      wx.showModal({       title: '是否授權(quán)當前位置',       content: '需要獲取您的地理位置,請確認授權(quán),否則無法獲取您所需數(shù)據(jù)',       success: function (res) {        if (res.cancel) {         that.setData({          isshowCIty: false         })         wx.showToast({          title: '授權(quán)失敗',          icon: 'success',          duration: 1000         })        } else if (res.confirm) {         wx.openSetting({          success: function (dataAu) {           if (dataAu.authSetting["scope.userLocation"] == true) {            wx.showToast({             title: '授權(quán)成功',             icon: 'success',             duration: 1000            })            //再次授權(quán),調(diào)用getLocationt的API            getLocation(that);           } else {            wx.showToast({             title: '授權(quán)失敗',             icon: 'success',             duration: 1000            })           }          }         })        }       }      })     } else if (res.authSetting['scope.userLocation'] == undefined) {//初始化進入      getLocation(that);     }     else { //授權(quán)后默認加載      getLocation(that);     }    }})

上述過程執(zhí)行順序為:

1.先加載wx.getLocation彈出自己的授權(quán)框,如圖

然后,點擊確定即可授權(quán),若點擊取消則取消授權(quán),當再次需要授權(quán)時,會調(diào)用我們自定義的Modal框,如圖

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 横峰县| 合阳县| 合山市| 宝山区| 黄梅县| 和龙市| 垫江县| 定襄县| 固始县| 安溪县| 仙桃市| 麻城市| 朔州市| 江源县| 临泽县| 师宗县| 吴忠市| 大荔县| 桓仁| 逊克县| 刚察县| 永川市| 静乐县| 嘉黎县| 恭城| 新绛县| 兰坪| 越西县| 天全县| 莱芜市| 金山区| 尚志市| 吴川市| 潼关县| 富锦市| 茂名市| 邓州市| 商城县| 蓬莱市| 上犹县| 峨边|