微信小程序获取当前所在城市

来源:互联网 发布:利达消防主机编程软件 编辑:程序博客网 时间:2024/06/05 09:29
data: {
imgs: [],
ak: "",//填写申请到的ak
province: '',
city: '',
district: '',
},
onLoad: function(options) {
this.getLocation();
},
getLocation: function() {
var that= this;
wx.getLocation({
type: 'wgs84',
success: function(res) {
var longitude= res.longitude;
var latitude= res.latitude;
that.loadCity(longitude,latitude);
}
});
},
loadCity: function(longitude,latitude) {
var that= this;
wx.request({
url: 'https://api.map.baidu.com/geocoder/v2/?ak='+ that.data.ak+ '&location='+ latitude + ','+ longitude+ '&output=json',
data: {},
header: {
'Content-Type':'application/json'
},
success: function(res) {
// success
console.log(res);
var province= res.data.result.addressComponent.province;
var city= res.data.result.addressComponent.city;
var district= res.data.result.addressComponent.district;
that.setData({
province: province,
city: city,
district: district,
});
},
fail: function() {
that.setData({city: "获取定位失败" });
},

})
},
原创粉丝点击