根据地址智能识别省市县/区

  • Post author:
  • Post category:其他




项目上遇到个需求,根据身份证地址或者用户输入的地址智能识别省市县/区

思路是:给到接口地址详情address,逐个匹配省province、市city、县/区area,然后将province+city+area返回即可,需要考虑一些情况:直辖市,address只有省、县/区,address只有市、县/区,市与县/区重名,县/区与县/区重名。

上代码:

查询 省province、市city、县/区area

-- province
SELECT
CASE
	WHEN
		temp.province LIKE '%省%' THEN
			substring_index( temp.province, '省', 1 ) 
			WHEN temp.province LIKE '%自治区%' THEN
			substring_index( temp.province, '自治区', 1 ) 
			WHEN temp.province LIKE '%市%' THEN
			substring_index( temp.province, '市', 1 ) 
		END AS provinceShort,
		temp.province AS provinceSource,
		temp.ids recordId 
	FROM
		( SELECT province, ids, count( DISTINCT province ) FROM city_dic GROUP BY province ) temp;

-- city
SELECT
	CASE
		WHEN
			temp.city LIKE '%市%' THEN
				substring_index( temp.city, '市', 1 ) 
				WHEN temp.city LIKE '%自治%' THEN
				substring_index( temp.city, '自治', 1 ) 
				WHEN temp.city LIKE '%盟%' THEN
				substring_index( temp.city, '盟', 1 ) 
				WHEN temp.city LIKE '%地区%' THEN
				substring_index( temp.city, '地区', 1 ) ELSE temp.city 
			END AS cityShort,
			temp.city AS citySource,
			temp.ids recordId 
		FROM
			( SELECT city, ids, count( DISTINCT city ) FROM city_dic GROUP BY city ) temp;

-- area

SELECT
		CASE
				
			WHEN
				LENGTH( temp.area ) < 7 THEN
					temp.area 
					WHEN temp.area = '津市市' THEN
					'津市' 
					WHEN temp.area = '辉县市' THEN
					'辉县' 
					WHEN temp.area LIKE '%自治%' THEN
					substring_index( temp.area, '自治', 1 ) 
					WHEN temp.area LIKE '%区%' THEN
					substring_index( temp.area, '区', 1 ) 
					WHEN temp.area LIKE '%县%' THEN
					substring_index( temp.area, '县', 1 ) 
					WHEN temp.area LIKE '%市%' THEN
					substring_index( temp.area, '市', 1 ) 
					WHEN temp.area LIKE '%旗%' THEN
					substring_index( temp.area, '旗', 1 ) 
				END AS areaShort,
				temp.area AS areaSource,
				temp.ids recordId 
		FROM
	( SELECT area, ids FROM city_dic ) temp;

逐个匹配省province、市city、县/区area (注:1、代码在jfinal框架下完成的,2、Record作用和Map<String,Object>类似)

//province
String province = "";
for(Record temp : provinces){
	String provinceShort = temp.getStr("provinceShort");
	if(addrStr.contains(provinceShort)){
		province = temp.getStr("provinceSource");
	}
}
//city
String city = "";

List<Record> cityRecords = new ArrayList<Record>();
for(Record temp : citys){
	String cityShort = temp.getStr("cityShort");
	if(addrStr.contains(cityShort)){
		cityRecords.add(temp);
	}
}
Record cityRecord = new Record();

// if onlyone city
if(cityRecords.size() == 1){
	city = cityRecords.get(0).getStr("citySource");
	cityRecord = cityRecords.get(0);
}else if(cityRecords.size() > 1){
	for(Record tempCityRecords : cityRecords){
		String cityId = tempCityRecords.getStr("recordId"); 
		//find city_dic by cityId
		Record cityRecordTemp = Db.findFirst("select * from city_dic where ids = ?", cityId);
		if(province.equals(cityRecordTemp.getStr("province"))){
			city = cityRecordTemp.getStr("city");
			cityRecord = cityRecordTemp;
		}
	}
}		

//area
String area = "";
List<Record> areaRecords = new ArrayList<Record>();
for(Record temp : areas){
	String areaShort = temp.getStr("areaShort");
	if(addrStr.contains(areaShort)){
		areaRecords.add(temp);
	}
}

判断 省、县/区

// if not empty province and empty city
if(StringUtils.isNotBlank(province) && StringUtils.isBlank(city)){
	if(areaRecords.size() == 1){
		String areaId = areaRecords.get(0).getStr("recordId");
		//find city by area
		Record areaRecordTemp = Db.findFirst("select * from city_dic where ids = ?", areaId);
		province = areaRecordTemp.getStr("province");
		city = areaRecordTemp.getStr("city");
		area = areaRecordTemp.getStr("area");
		if(province.equals("北京市")||province.equals("上海市")||province.equals("天津市")||province.equals("重庆市")){
			province = "";
		}
		return province + city + area;
	} else if (areaRecords.size() > 1) {
		for(Record tempAreaRecords : areaRecords){
			String areaId = tempAreaRecords.getStr("recordId"); 
			//find city by area
			Record areaRecordTemp = Db.findFirst("select * from city_dic where ids = ?", areaId);
			if(province.equals(areaRecordTemp.getStr("province"))){
				city = areaRecordTemp.getStr("city");
				area = areaRecordTemp.getStr("area");
				if(province.equals("北京市")||province.equals("上海市")||province.equals("天津市")||province.equals("重庆市")){
					province = "";
				}
				return province + city + area;
			}
		}
	} else {
		if(province.equals("北京市")||province.equals("上海市")||province.equals("天津市")||province.equals("重庆市")){
			province = "";
		}
		return province + city + area;
	}
}

判断 市、县/区

// if empty province and not empty city
if(StringUtils.isBlank(province) && StringUtils.isNotBlank(city)){
	String cityId = cityRecord.getStr("recordId");
	//find province by city
	cityRecord = Db.findFirst("select * from city_dic where ids = ?", cityId);
	province = cityRecord.getStr("province");
}

最后

if(areaRecords.size() == 1){
	area = areaRecords.get(0).getStr("areaSource");
} else if (areaRecords.size() > 1) {
	for(Record tempAreaRecords : areaRecords){
		String areaId = tempAreaRecords.getStr("recordId"); 
		//find city by area
		Record areaRecordTemp = Db.findFirst("select * from city_dic where ids = ?", areaId);
		if(province.equals(areaRecordTemp.getStr("province"))){
			city = areaRecordTemp.getStr("city");
			area = areaRecordTemp.getStr("area");
			if(province.equals("北京市")||province.equals("上海市")||province.equals("天津市")||province.equals("重庆市")){
				province = "";
			}
			return province + city + area;
		}
	}
}

结束,将province+city+area返回即可


Enjoying!

附:建表语句:

CREATE TABLE `city_dic` (
  `ids` varchar(32) NOT NULL,
  `p_code` varchar(100) NOT NULL COMMENT '省编号',
  `province` varchar(255) DEFAULT NULL COMMENT '省',
  `c_code` varchar(100) DEFAULT NULL COMMENT '市编号',
  `city` varchar(255) DEFAULT NULL COMMENT '市',
  `a_code` varchar(100) DEFAULT NULL COMMENT '区编号',
  `area` varchar(255) DEFAULT NULL COMMENT '区',
  `status` tinyint(2) DEFAULT NULL,
  PRIMARY KEY (`ids`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='省市区';

附:成果展示

在这里插入图片描述

在这里插入图片描述



版权声明:本文为chenqitazhishi原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。