小程序获取当前精确位置

第一步:设置app.json: 

"permission": {
    "scope.userLocation": {
        "desc": "用于提供位置信息水印拍摄"
    }
  },
"requiredPrivateInfos": [
    "getLocation"//这个需要在小程序后台:开发-开发管理-接口设置 中开通
  ]

 第二步:去腾讯地图中申请自己的key

        记得要勾选WebServiceApi

 

 

腾讯位置服务 - 立足生态,连接未来 

第三步:下载sdk文件(选择你需要的版本并放到你的项目文件中)

微信小程序JavaScript SDK | 腾讯位置服务

第四步: 在页面中进行配置

        ①

var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
var qqmap;

         ②

data: {
    myLatitude: "",
    myLongitude: "",
    myAddress: "",
}

        ③

onLoad() {
    qqmap = new QQMapWX({
       key:app.globalData.keyVal    //你自己的key
    });
    var that = this
    wx.getLocation({
      type:"gcj02",
      altitude:true,
      isHighAccuracy:true,
      highAccuracyExpireTime:2000,
      success(res){
        console.log('ddddd',res)
        that.setData({myLatitude: res.latitude, myLongitude: res.longitude})
        //用腾讯地图的api,根据经纬度获取城市
        qqmap.reverseGeocoder({
          location: {
            latitude: that.data.myLatitude,
            longitude: that.data.myLongitude
          },
          get_poi: 1,
          poi_options: 'policy=1;radius=1000;page_size=30;page_index=1',
          success: function (res) {
            console.log(res)
            var a = res.result.address_component
            //获取市和区(区可能为空)
            that.setData({myAddress: a.city + a.district})
            //控制台//输出结果 
            console.log(that.data.myAddress) 
          },
          fail:function(res) {
            console.log(res);
          },
          complete: function(res) {
            //console.log(res);
          }
        // console.log('获取位置:',res)
          })
      }
    })
}

结果:(#^.^#)

 当然这个是在开发工具上的结果,精准结果还是得用手机看

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值