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: "获取定位失败" });
},
})
} ,