getpos.js 1.29 KB
// components/userinfo/getpos.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {},

  /**
   * 组件的初始数据
   */
  data: {},
  attached(){
    //this.getPos()
  },
  /**
   * 组件的方法列表
   */
  methods: {
    getPos(){
      this._getPos().then(res => {
        console.log('wx.getLocation.then', res);
        this.triggerEvent("pos",res);
      }).catch((e) => {
        console.log('wx.getLocation.catch', e);
        if ('getLocation:fail auth deny' == e.errMsg
          || 'getLocation:fail:auth denied' == e.errMsg
          || 'getLocation:fail authorize no response' == e.errMsg) {
          this.selectComponent('#showModal').options({
            header: '定位失败',
            body: '需要地理位置授权'
          }).show();
        } else {
          setTimeout(() => {
            this.getPos()
          }, 500)
        }

      })
    },
    _getPos(){
      return new Promise((success, fail) => {
        wx.getLocation({
          type: 'gcj02',
          success: (res) => {
            success(res)
          },
          fail: (res) => {
            fail(res)
          },
          complete: (res) => {
            console.log('wx.getLocation', res)
          }
        });
      })
    },
    showModal(e){
      wx.openSetting()
    },
  }
})