uniapp 在h5和小程序上使用高德定位用户城市

get_addr.js

const amap_wx_key = '<高德小程序key>';
const amap_h5_key = '<web服务key>';

// 小程序需要配置 restapi.amap.com 为合法域名
export function get_addr() {
  return new Promise((_res, _rej) => {
    uni.getLocation({
      success: function (pos) {
        uni.request({
          method: 'GET',
          url: 'https://restapi.amap.com/v3/geocode/regeo',
          data: {
            key: amap_h5_key,
            location: `${pos.longitude},${pos.latitude}`,
            poitype: '城市',
          },
          success: ({
            data
          }) => {
            console.log(data);
            _res(data.regeocode.addressComponent);
          },
          fail: r => {
            _rej(r);
          }
        });
      }
    });
  })
}
posted @ 2020-07-23 10:55  Ajanuw  阅读(3478)  评论(0编辑  收藏  举报