首页 > 微信 > 详细

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

时间:2020-07-23 15:41:07      阅读:336      评论:0      收藏:0      [点我收藏+]
import amap from ‘@/static/lib/amap-wx.js‘; // 只能用于小程序


      const amap_wx_key = ‘<微信key>‘;
      const amap_h5_key = ‘<Web服务key>‘;

      let _key = ‘‘;
      // #ifdef H5
      _key = amap_h5_key

      uni.getLocation({
        success: function(pos) {
          uni.request({
            method: ‘GET‘,
            url: ‘https://restapi.amap.com/v3/geocode/regeo‘,
            data: {
              key: _key,
              location: `${pos.longitude},${pos.latitude}`,
              poitype: ‘城市‘,
            },
            success: ({
              data
            }) => {
              const city = data.regeocode.addressComponent.city
              commit(‘setCity‘, city)
              res(city);
            },
            fail: r => {
              console.log(r);
            }
          });
        }
      });
      // #endif

      // #ifdef MP-WEIXIN
      _key = amap_wx_key
      this.amapPlugin = new amap.AMapWX({
        key: _key
      });
      this.amapPlugin.getRegeo({
        success: data => {
          const city = data[0].regeocodeData.addressComponent.city;
          commit(‘setCity‘, city)
          res(city);
        },
        fail(e) {
          console.error(e);
          uni.showModal({
            title: e.errCode,
            content: e.errMsg,
          })
        }
      });
      // #endif

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

原文:https://www.cnblogs.com/ajanuw/p/13365054.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!