zoukankan      html  css  js  c++  java
  • 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
    
  • 相关阅读:
    python10.31
    python10.29
    python10.28
    python10.27
    python10.25
    python10.24
    python10.23
    四边形不等式与决策单调
    0x57~0x59
    0x55~0x56
  • 原文地址:https://www.cnblogs.com/ajanuw/p/13365054.html
Copyright © 2011-2022 走看看