今天写一个项目的时候遇到在选择位置后需要拿到选择的位置的省市区传给后端做校验,但是uniapp的uni.chooseLocation只能拿到经纬度以及详细地址和名称,拿不到省市区,下面呢是我用的根据经纬度转化为省市区的方法,希望对大家有用,上代码········对了这个一定要运行到手机上看哦,在电脑上是不可以的
uni.chooseLocation({
type: 'gcj02',
geocode:true,
success: function (res) {
console.log(res,'chooseLocation');
_this.shop_address = res.address;
var point = new plus.maps.Point(res.longitude, res.latitude);
plus.maps.Map.reverseGeocode(
point,
{},
function(event) {
var address = event.address; // 转换后的地理位置
var point = event.coord; // 转换后的坐标信息
var coordType = event.coordType; // 转换后的坐标系类型
console.log(address, 'address');
console.log(point);
var reg = /.+?(省|市|自治区|自治州|县|区)/g;
console.log(address.match(reg));
let addressList=address.match(reg).toString().split(",");
console.log(addressList[0]);
console.log(addressList[1]);
console.log(addressList[2]);
// _this.shopAddress = addressList[0]+addressList[1]+addressList[2]
_this.province = addressList[0]
_this.city = addressList[1]
_this.district = addressList[2]
_this.longitude = point.longitude
_this.latitude = point.latitude
console.log(address.substring(_this.shopAddress.length,address.length))
},
function(e) {}
)
}
})