步骤一:地址数据源address-data.js-放在我的资源里面了
步骤二:HTML
<u-picker keyName="name" :show="addressShow" ref="uPicker" @cancel="pickerCancel" :columns="columns"
@confirm="confirm" @change="changeHandler">
</u-picker>
步骤二:js
import {
areaList
} from '@/common/address-data.js'
form:{
where:'',
},
addressShow: false,
getLocationState: false,
userAddress: {},
addressShow: false,
getAddressText: "",
columns: [
[],
[],
[]
],
created() {
console.log(areaList)
this.columns[0] = areaList;
this.columns[1] = areaList[0].cities;
this.columns[2] = areaList[0].cities[0].districts;
},
getisshow() {
this.addressShow = true
},
pickerCancel() {
this.addressShow = false;
this.$refs.uPicker.setIndexs([0, 0, 0], true);
this.$refs.uPicker.setColumnValues(0, areaList);
this.$refs.uPicker.setColumnValues(1, areaList[0].cities);
this.$refs.uPicker.setColumnValues(2, areaList[0].cities[0].districts);
},
changeHandler(e) {
const {
columnIndex,
value,
values,
index,
indexs,
picker = this.$refs.uPicker
} = e
if (columnIndex === 0) {
picker.setColumnValues(1, areaList[index].cities)
picker.setColumnValues(2, areaList[indexs[0]].cities[0].districts)
} else if (columnIndex === 1) {
picker.setColumnValues(2, areaList[indexs[0]].cities[indexs[1]].districts)
}
},
confirm(e) {
console.log('confirm', e)
this.userAddress.province = e.value[0].name;
this.userAddress.city = e.value[1].name;
this.userAddress.area = e.value[2].name;
this.userAddress.regionId = e.value[2].id;
this.form.where = this.userAddress.province + ' ' + this.userAddress.city + ' ' + this.userAddress
.area;
this.pickerCancel();
},
附赠一个有需要的时候用的代码
poptanqi() {
let that = this;
if (!that.getLocationState) {
uni.showLoading({
title: '加载中'
});
uni.getLocation({
type: 'wgs84',
isHighAccuracy: true,
success: function(res) {
console.log('当前位置的经度:' + res.longitude);
console.log('当前位置的纬度:' + res.latitude);
getAdInfoByLngAndLat({
longitude: res.longitude,
latitude: res.latitude
}).then(res2 => {
if (res2.data.head.code == '0') {
let resData = res2.data.details;
if (resData.status == 0) {
that.$set(that.userAddress, 'province', resData.result.ad_info
.province);
that.$set(that.userAddress, 'city', resData.result.ad_info
.city);
that.$set(that.userAddress, 'area', resData.result.ad_info
.district);
that.$set(that.userAddress, 'regionId', resData.result.ad_info
.adcode + '000');
that.getLocationState = true;
} else {
that.$nextTick(() => {
that.$refs["cityPicker"].show();
})
}
} else {
that.$nextTick(() => {
that.$refs["cityPicker"].show();
})
}
});
},
fail(err) {
that.addressShow = true;
console.log('遇到错误!');
console.log(err);
},
complete() {
uni.hideLoading();
}
});
} else {
that.$nextTick(() => {
this.$refs["cityPicker"].show();
})
}
},