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