腾讯地图获取位置信息

2019-03-05  本文已影响0人  一个小前端程序员

1.首先在app.json中添加

 "permission": {
    "scope.userLocation": {
      "desc": "你的位置信息将用于城市定位"
    }
  },
//2019年1月14日起新提交发布的版本若未填写地理位置用途说明,则将无法正常调用地理位置相关接口,请及时填写地理位置用途说明。

2.获取经纬度

page({
//获取经纬度
  getLocation:function(){
    var that = this
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        that.getLocal(latitude, longitude)
      }
    })
  },
})

3.在微信公众平台>小程序开发设置>添加服务器域名:https://apis.map.qq.com
4.下载微信小程序JavaScriptSDK,地址:http://3gimg.qq.com/lightmap/xcx/jssdk/qqmap-wx-jssdk1.1.zip,并且引入项目中
5.在代码中使用腾讯地图

var QQMapWX = require('../../libs/qqmap-wx-jssdk.min.js');
// 实例化API核心类
var qqmapsdk = new QQMapWX({
  key: '你的key' // 必填
});

page({
// 获取当前地理位置
  getLocal: function (latitude, longitude) {
    let that = this;
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude
      },
      success: function (res) {
        let district = res.result.ad_info.district
        that.setData({
          district: district
        })
        that.getData();
      },
      fail: function (res) {
        console.log(res);
      },
      complete: function (res) {
        // console.log(res);
      }
    });
  },
})

详情参考:https://lbs.qq.com/qqmap_wx_jssdk/index.html

上一篇 下一篇

猜你喜欢

热点阅读