微信小程序定位用户所在城市

1,135 阅读1分钟

复杂:getCityByCheck

  1. 首次检查权限,没有权限则弹出系统定位提示框;
  2. 非首次查询权限,权限为拒绝后则弹出确认框,确定->去设置页面,返回简单查询结果,不确定->返回'';
  3. 有权限-> 简单查询结果。

简单: getCity

  1. 获取经纬度之后地址逆解析。

// 获取定位权限
const checkLocation = () => {
  //选择位置,需要用户授权
  return new Promise((resolve, reject) => {
    wx.getSetting({
      success:(res) => {
        if (res.authSetting['scope.userLocation']== false) { // 请求过了,用户拒绝定位
          resolve(false);
        } else {
          wx.authorize({
            scope: 'scope.userLocation',
            success:() => {
              resolve(true);
            },fail:() => {
              reject();
            }
          })
        } 
      }
    })
  })
}

// 打开定位权限
const openSetting = (content = '请打开定位,以显示当前城市可配送商品!') => {
  return new Promise((resolve) => {
    wx.showModal({
      title: '提示', //提示的标题,
      content: content, //提示的内容,
      showCancel: true, //是否显示取消按钮,
      cancelText: '取消', //取消按钮的文字,默认为取消,最多 4 个字符,
      cancelColor: '#000000', //取消按钮的文字颜色,
      confirmText: '确定', //确定按钮的文字,默认为取消,最多 4 个字符,
      confirmColor: '#3CC51F', //确定按钮的文字颜色,
      success: res => {
        if (res.confirm) {
          // openSetting 是需要事件驱动的,保证它的同步性。
          wx.openSetting({
            success() {              
              res.authSetting = { "scope.userLocation": true  };
              resolve(true);
            },
            fail() {
              resolve(false)
            }
          })
        } else if (res.cancel) {
          resolve(false);
        }
      }
    });
  });
};

// 获取经纬度
const getLocation = () => {
  return new Promise((resolve, reject) => {
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        resolve(res);
      },
      fail(err) {
        reject(err)
      }
    })
  })
};

// 地址逆解析获取城市
const getCityByLocation = (location) => {
  return new Promise ((resolve,reject)=>{
    wx.request({
      url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${location.latitude},${location.longitude}&key=LIMBZ-LTCWU-U65VE-2L5SW-N54I7-X6FAW`,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        resolve(res.data.result.address_component.city);
      },
      fail() {
        resolve('');
      }
    })
  })
};

// 简单获取城市
const getCity = () => {
  return getLocation().then((res) => {
    return getCityByLocation(res);
  }).catch(() => {
    return ''
  });
};

// 开启定位权限提示获取定位
const getCityByCheck = (content = '定位') => {
  return new Promise((resolve) => {
    checkLocation().then((locationRole) => {
      if (locationRole) {
        getCity().then((res) => {
          resolve(res);
        });
      } else {
        openSetting(content).then((openSetting) => {
          if (openSetting) {
            getCity().then((res) => {
              resolve(res);
            });
          } else {
            resolve('');
          }
        })
      }
    }).catch(() => {
      resolve('');
    })
  })
   
}



module.exports = {
  checkLocation,
  openSetting,
  getLocation,
  getCityByLocation,
  getCity,
  getCityByCheck
}