小程序的首次登陆 小程序的生命周期开始 授权地理位置的页面 js 直接调用微信官方 腾讯逆解析地址 方法

//app.js   
var QQMapWX = require('utils/qqmap-wx-jssdk.js');
var qqmapsdk;
App({
  onLaunch: function () {
    // 实例化API核心类
    qqmapsdk = new QQMapWX({
      key: 'TCCBZ-J67W4-FWTUL-DQEEN-FHM2K-3CBGZ'
    });
    // 获取定位
    this.getLocation(function () { });
    // 初始化慧眼实名核身组件
    const Verify = require('/verify_mpsdk/main');
    Verify.init();
  },

  /**
   * 全局变量
   */
  globalData: {
    // serverUrl: 'https://www.xpms.cn/mini',
    serverUrl: 'https://192.168.1.20:8080/mini',
    uploadUrl: 'https://192.168.1.5:9090/mini/upload',
    imageUrl: 'https://192.168.1.20:8080/file/mini',
    userInfo: {
      address: {}
    },
    servicePhone: '400 0719 828',
    hotel_group_id: 1
  },
// 全局的方法 在页面page中调用 结果 可能会在 onload之后 才生效 所以 要 调用方法 传入回调才能用到最新的全局数据 //获取用户地理位置 getLocation: function (callBack) { var that = this; wx.getLocation({ success: function (res) { //2、根据坐标获取当前位置名称,显示在顶部:腾讯地图逆地址解析 qqmapsdk.reverseGeocoder({ location: { latitude: res.latitude, longitude: res.longitude }, success: function (addressRes) { that.globalData.userInfo.address.nation = addressRes.result.address_component.nation; that.globalData.userInfo.address.province = addressRes.result.address_component.province; that.globalData.userInfo.address.city = addressRes.result.address_component.city; that.globalData.userInfo.address.district = addressRes.result.address_component.district; that.globalData.userInfo.address.street = addressRes.result.address_component.street; that.globalData.userInfo.address.street_number = addressRes.result.address_component.street_number; that.globalData.userInfo.address.lat = addressRes.result.location.lat; that.globalData.userInfo.address.lng = addressRes.result.location.lng; that.globalData.userInfo.address.address = addressRes.result.address; callBack(); } }) }, fail: function () { wx.getSetting({ success: function (res) { var statu = res.authSetting; if (!statu['scope.userLocation']) { wx.showModal({ title: '是否授权当前位置', content: '需要获取您的地理位置,请确认授权,否则地图功能将无法使用', success: function (tip) { if (tip.confirm) { wx.openSetting({ success: function (data) { if (data.authSetting["scope.userLocation"] === true) { //授权成功之后,再调用chooseLocation选择地方 wx.getLocation({ success: function (res) { //2、根据坐标获取当前位置名称,显示在顶部:腾讯地图逆地址解析 qqmapsdk.reverseGeocoder({ location: { latitude: res.latitude, longitude: res.longitude }, success: function (addressRes) { that.globalData.userInfo.address.nation = addressRes.result.address_component.nation; that.globalData.userInfo.address.province = addressRes.result.address_component.province; that.globalData.userInfo.address.city = addressRes.result.address_component.city; that.globalData.userInfo.address.district = addressRes.result.address_component.district; that.globalData.userInfo.address.street = addressRes.result.address_component.street; that.globalData.userInfo.address.street_number = addressRes.result.address_component.street_number; that.globalData.userInfo.address.lat = addressRes.result.location.lat; that.globalData.userInfo.address.lng = addressRes.result.location.lng; that.globalData.userInfo.address.address = addressRes.result.address;
                    // 在page页面中传入函数 进行回调用 是为了 防止在page页面里执行的函数所用的数据不是最新的 因为 wx.golocaltion 等 是网络请求 在页面中调用 会 在 onload()生命周期 页面加载后 才得到结果 callBack(); } }) }, }) } else { wx.showToast({ title: '授权失败', icon: 'none', duration: 1000 }) } } }) } } }) } }, fail: function (res) { wx.showToast({ title: '调用授权窗口失败', icon: 'none', duration: 1000 }) } }) } }) }, })

  微信小程序获取地理位置授权 参考: https://www.cnblogs.com/memphis-f/p/11535970.html     普通方法        参考  https://blog.csdn.net/harry_yaya/article/details/88365706    参考  https://www.jianshu.com/p/8fab41a6951e

    https://developers.weixin.qq.com/minigame/dev/api/open-api/setting/wx.getSetting.html

苦心人,天不负
原文地址:https://www.cnblogs.com/taxun/p/13530809.html