小程序定位功能误甚,雖然功能也完善振坚,但是首次接觸還是挺疑惑,看了下邏輯還是明白些了危融,總結(jié)如下:
一鞋怀、微信開發(fā)者文檔:
wx.getLocation(Object object)鏈接地址:https://developers.weixin.qq.com/miniprogram/dev/api/location/wx.getLocation.html
二双泪、代碼貼上:
selfLocation() {
const self = this
uni.showLoading({
title: '定位中',
mask: true,
});
uni.getLocation({
type: 'gcj02',
success: (res) => {
let latitude, longitude;
latitude = res.latitude.toString();
longitude = res.longitude.toString();
this.latitude = res.latitude
this.longitude = res.longitude
getGeocoder({lat: latitude,long: longitude}).then(res => { // 獲取詳細(xì)信息的接口
const data = res.data;
self.userAddress.userAddressdetail = ''
var params = {
text: data.address
}
parseAddress(params).then(res => { // 粘貼詳細(xì)信息的接口
console.log(res)
if(res.status==200 && res.message=="success"){
this.$forceUpdate(); // 定位后,界面沒有反應(yīng)密似,因此加上強(qiáng)制刷新
this.userAddress.userAddressdetail = res.data.town + res.data.detail;
this.$set( this.userAddress, 'selectAddress', parseInt(res.data.county_info.city_id));
this.addressInfo[0] = res.data.province_info ? res.data.province_info : {};
this.addressInfo[1] = res.data.city_info ? res.data.city_info : {};
this.addressInfo[2] = res.data.county_info ? res.data.county_info : {};
}
}).catch(res => {
console.log("沒有地址信息")
})
uni.hideLoading();
})
},
fail: (res) => {
console.log(res)
uni.hideLoading();
uni.showToast({
title: res.errMsg,
icon: 'none',
duration: 1000
});
}
});
},
二焙矛、注意事項(xiàng):點(diǎn)擊定位按鈕,界面沒有反應(yīng)辛友,但是點(diǎn)擊返回薄扁,確定位成功,因此加上this.$forceUpdate()即可废累;
三邓梅、報(bào)錯(cuò):getLocation:fail the api need to be declared in the requiredPrivateInfos
經(jīng)查發(fā)現(xiàn):檢查app.json的配置,發(fā)現(xiàn)無誤
"permission" : {
"scope.userLocation" : {
"desc" : "獲取您的位置"
}
},
編輯器問題消失邑滨,手機(jī)體驗(yàn)版依然有問題日缨,問題在繼續(xù)解決中。掖看。匣距。。
查找資料發(fā)現(xiàn)哎壳,手機(jī)體驗(yàn)版不可以開啟定位毅待,只能正式版。