uniapp H5高德地图获取当前位置

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
      // 小程序需要配置 restapi.amap.com 为合法域名
      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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容