美文网首页程序员小程序
微信小程序获取用户的具体位置信息

微信小程序获取用户的具体位置信息

作者: IT锟 | 来源:发表于2017-10-24 13:23 被阅读1789次

要想通过微信小程序获得用户的准确位置,大体的步骤就两步,由于微信小程序只能获取到经纬度,所有需要用到第三方的API来反查询用户的地理位置,具体步骤如下:

1、获取经纬度

wx.getLocation({
      success: function (res) {
        console.log(res)
    //保存到data里面的location里面
        that.setData({
          location: {
            longitude: res.longitude,  
            latitude: res.latitude
          }
        })

      }
    })

2、根据经纬度取得位置信息

这一步需要用到第三方的api,这里我选择用腾讯自己家的地图,去他的官网 http://lbs.qq.com 注册下,就可以了,不需要认证啥的。

图片.png

然后就可以使用了,代码如下:


  var qqMapApi = 'http://apis.map.qq.com/ws/geocoder/v1/' + "?location=" + that.data.location.latitude + ',' +
      that.data.location.longitude + "&key=你的腾讯地图的key" + "&get_poi=1";

    wx.request({
      url: qqMapApi,
      data: {},
      method: 'GET',
      success: (res) => {
        console.log(res.data)

      //取位置名
        that.setData({
          loca: res.data.result.address
        })
      }
    });

[获取授权]

相关文章

网友评论

    本文标题:微信小程序获取用户的具体位置信息

    本文链接:https://www.haomeiwen.com/subject/opbtpxtx.html