1. 程式人生 > >微信小程式自動定位,通過百度地圖根據經緯度獲取該地點所在城市資訊

微信小程式自動定位,通過百度地圖根據經緯度獲取該地點所在城市資訊

微信小程式獲得經緯度

var that = this
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        console.log(res)
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
    }
})

百度地圖api

'https://api.map.baidu.com/geocoder/v2/?location=' + res.latitude+',' + res.longitude+'&output=json&pois=1&ak='+ baiduAK{註冊百度地圖api獲得的accessKey}  http://lbsyun.baidu.com/
  返回json裡的city欄位就是城市資訊了。