uni.getLocation({
type: 'gcj02',
success: function(res) {
console.log('当前位置的经度:' + res.longitude);
console.log('当前位置的纬度:' + res.latitude);
uni.chooseLocation({
keyword: '请选择位置',
longitude: res.longitude,
latitude: res.latitude,
success: res => {
console.log('详细地址:' + res.address);
console.log('纬度:' + res.latitude);
console.log('经度:' + res.longitude);
var reg = /.+?(省|市|自治区|自治州|行政区|盟|旗|县|区)/g; // 省市区的正则
let area = res.address.match(reg); // 分割省市区
var len = 0;
for (var i = 0; i < area.length; i++) {
// 获取省市区的长度
len += area[i].length;
}
area.push(res.address.substring(len)); // 获取省市区之后的字符串
console.log(area); // ['内蒙古自治区', '锡林郭勒盟', '正镶白旗', '前进大街2699号']
let tojian = area[0].split('');
let condition = tojian[tojian.length - 1];
let province = '';
let city = '';
let areas = '';
if (condition == '省') {
province = area[0];
city = area[1];
areas = area[2];
} else {
province = area[0];
city = area[0];
areas = area[1];
}
let longitude = res.longitude;
let latitude = res.latitude;
let obj = {
longitude,
latitude,
province,
city,
area: areas,
petArchivesId,
portrait
};
},fail: (err) => {
console.log('打开地图位置失败', err);
this.$u.toast('打开地图位置失败');
},
complete: (lete) => {
console.log("都会走的",lete);
}
});
},
fail: err => {
console.log('获取当前位置失败', err);
this.$u.toast('获取位置失败');
}
});