使用uniapp
地图选择位置的时候,返回给我们的地址是合并在一起的,但是后台保存地址都是单独字段保存,所以需要把地址根据省市区详细地址拆分出来。
getArea(str) {
let area = {}
const index1 = str.indexOf("省")
if (index1 != -1) {
area.province = str.substring(0, index1 + 1)
str = str.substr(index1 + 1)
}
const index = str.indexOf("自治区")
if (index != -1) {
area.province = str.substring(0, index + 3)
str = str.substr(index + 3)
}
let index2 = str.indexOf("市")
if (index2 != -1) {
area.city = str.substring(0, index2 + 1)
str = str.substr(index2 + 1)
}
let index3 = str.lastIndexOf("区")
if (index3 != -1) {
area.area = str.substring(0, index3 + 1)
str = str.substr(index3 + 1)
}
let index31 = str.lastIndexOf("县")
if (index31 != -1) {
area.area = str.substring(0, index31 + 1)
str = str.substr(index31 + 1)
}
let index4 = str.lastIndexOf("镇")
if (index4 != -1) {
area.zheng = str.substring(0, index4 + 1)
str = str.substr(index4 + 1)
}
area.detailed = str
return area;
}
console.log(this.getArea("内蒙古自治区乌兰浩特市二区"));
console.log(this.getArea("重庆市长寿区晏中路与松柏站街交叉口西100米"));
console.log(this.getArea("贵州省毕节市威宁彝族回族苗族自治县海边街道滨海大道奥园广场肯德基旁"));
console.log(this.getArea("重庆市渝中区中兴路"));
console.log(this.getArea("湖北省黄石市阳新县"));
console.log(this.getArea("湖北省宜昌市长阳土家族自治县"));
console.log(this.getArea("重庆市秀山土家族苗族自治县武陵南路武陵广场"));
console.log(this.getArea("湖北省宜昌市长阳土家族自治县"));