uni 获取当前位置 + jsonp跨域(记得在uni-web配置腾讯地图key)

腾讯位置

// 5.在onLoad里注册
      this.qqmapsdk = new QQMapWX({
        // 腾讯位置平台申请的秘钥
        key: 'ZwTVu16RLXjhW7FHDjYt5HfMnR1dhFpR'
      })
//6.获取经纬度
    getJW () { // 获取经纬度
      let _this = this
      uni.getLocation({
        type: 'wgs84',
        geocode: true,
        success: function (res) {
          console.log(res);
          console.log('当前位置的经度:' + res.longitude);
          console.log('当前位置的纬度:' + res.latitude);
          _this.getLocal(res.longitude, res.latitude)
        },
      });
    },
// 7.根据经纬度获取地理位置
      getLocal (longitude, latitude) { // 根据经纬度获取地理位置
      let locationdata = latitude + ',' + longitude // 经纬度

// jsonp 解决跨域
      this.$jsonp('https://api.map.baidu.com/geocoder/v2/?callback=renderReverse&output=json&pois=1', {
        ak: 'ZwTVu16RLXjhW7FHDjYt5HfMnR1dhFpR', // 腾讯地图key
        location: locationdata
      }).then((res) => {
        console.log(res)
      })
    },

跨域问题 Jsonp 解决方案

  • 1.下包 npm install vue-jsonp --save

  • 2.在 min.js 引入

    • import { VueJsonp } from 'vue-jsonp'

    • Vue.use(VueJsonp)

  • 3.使用

    • this.$jsonp()

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值