getIP: function (e) {
var that = this;
wx.request({
/* //方法1
url: "https://www.jeremy7.cn/springboot-schedule/holiday/ip-address",
success(res) {
var data = res.data;
that.setData({
localAddress: data,
localIp: data.ip
})
console.log("IP:",that.data.localIp)
},
fail: console.error
*/
//方法2
url: "http://ip-api.com/json",
success: function (e) {
that.localIp = e.data.query
that.setData({
localIp: e.data.query,
})
//通过ip获取当前经纬度
wx.request({
url: 'http://ip-api.com/json/' + that.localIp + '?lang=zh-CN',
method: 'GET',
header: {
'content-type': 'application/x-www-form-urlencoded'
},
success: function (res) {
let lat = res.data.lat; //纬度
let lon = res.data.lon; //经度
let regionName = res.data.regionName; //城市 'http://ip-api.com/json/' + that.localIp
console.log("wx.request返回数据=>", that.localIp,"regionName:", regionName, "lat=", lat, "lon=", lon)
}
});
},
fail: console.error
})
}
测试结果:IP地址正确,省份正确,城市可能有错误(怀疑原因是因为得到的经纬度错误)