zl程序教程

您现在的位置是:首页 >  工具

当前栏目

微信小程序 获取地理位置信息

微信程序 获取 信息 地理位置
2023-09-11 14:16:38 时间

app.json

"permission":{
  "scope.userLocation": {
    "desc": "你的位置信息将用于小程序位置接口的效果展示"
  }
},

index.js

onLoad: function (options) {
    this.getLocationDetail();
  },

  getLocationDetail () {
    wx.getLocation({
      type: 'wgs84',
      success: res => {
        const latitude = res.latitude
        const longitude = res.longitude
        console.log("lat:" + latitude + ",lon:" + longitude)
        this.getCity(latitude, longitude);
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  getCity (latitude, longitude) {
    wx.request({
        url: "http://api.map.baidu.com/reverse_geocoding/v3/",
        data: {
          ak: "你的ak密钥百度随便申请",
          output: "json",
          location: latitude + "," + longitude
        },
      success: res => {
        console.log(res)
      },
    })
  },