关于小程序城市定位的问题

onLoad: function (options) {

this.loadInfo();

},
loadInfo: function () {

var page = this
wx.getLocation({
  type: 'wgs84', // 默认为 wgs84 返回 gps 坐标,gcj02 返回可用于 wx.openLocation 的坐标  
  success: function (res) {
    // success  
    var longitude = res.longitude
    var latitude = res.latitude
    page.loadCity(longitude, latitude)
  },
  fail: function () {
    // fail  
  }
})

},
loadCity: function (longitude, latitude) {

var page = this
wx.request({
  url: 'https://webapi.amap.com/maps?v=1.4.2&key=4374d65645b4c2b2586aa502b111e125 &location=' + latitude + ',' + longitude + '&output=json',
  data: {},  
  header: {
    'Content-Type': 'application/json'
  },
  success: function (res) {
    // success    
    console.log(res);
    var city = res.data.result.addressComponent.city;
    page.setData({ currentCity: city });
  },
  fail: function () {  
    page.setData({ currentCity: "获取定位失败" });
  }, 
})

} ,图片描述

阅读 1.7k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题