解析高德地图api获取省市区,生成最新三级联动sql表
程序员文章站
2022-04-28 13:07:21
前言: 最近项目中用到了全国省市区三级信息,但是网上找到的信息都是比较旧的信息。与现在最新的地区信息匹配不上。后来想到高德地图上可能有这些信息。所以解析了一下api接口,生成了相关省市区的sql信息。(注意:这里面没有港台地区)具体的请参看高德地图文档。 alibaba.fastjson解析+mys ......
前言:
最近项目中用到了全国省市区三级信息,但是网上找到的信息都是比较旧的信息。与现在最新的地区信息匹配不上。后来想到高德地图上可能有这些信息。所以解析了一下api接口,生成了相关省市区的sql信息。(注意:这里面没有港台地区)具体的请参看高德地图文档。
alibaba.fastjson解析+mysql表
1、高德地图api调用接口说明文档:
具体数据接口:
https://restapi.amap.com/v3/config/district?subdistrict=4&key=bb4198a1f146184af53322d424732f6b
注意:如果想调用此接口需要申请一个key,免费的,注册就可以获取
2、我的码云代码:
3、java代码如下:
@requestmapping("/getall") public object getmasterlist() throws exception { httpclientresult result = httpclientutils.doget("https://restapi.amap.com/v3/config/district?subdistrict=4&key=bb4198a1f146184af53322d424732f6b"); string jsonstring = result.getcontent(); //数据结构如下 /* string jsonstring = "{\n" + " \"status\": \"1\",\n" + " \"info\": \"ok\",\n" + " \"infocode\": \"10000\",\n" + " \"count\": \"1\",\n" + " \"suggestion\": {\n" + " \"keywords\": [],\n" + " \"cities\": []\n" + " },\n" + " \"districts\": [{\n" + " \"citycode\": [],\n" + " \"adcode\": \"100000\",\n" + " \"name\": \"*\",\n" + " \"center\": \"116.3683244,39.915085\",\n" + " \"level\": \"country\",\n" + " \"districts\": [{\n" + " \"citycode\": [],\n" + " \"adcode\": \"410000\",\n" + " \"name\": \"河南省\",\n" + " \"center\": \"113.665412,34.757975\",\n" + " \"level\": \"province\",\n" + " \"districts\": [{\n" + " \"citycode\": \"0393\",\n" + " \"adcode\": \"410900\",\n" + " \"name\": \"濮阳市\",\n" + " \"center\": \"115.041299,35.768234\",\n" + " \"level\": \"city\",\n" + " \"districts\": [{\n" + " \"citycode\": \"0393\",\n" + " \"adcode\": \"410923\",\n" + " \"name\": \"南乐县\",\n" + " \"center\": \"115.204336,36.075204\",\n" + " \"level\": \"district\",\n" + " \"districts\": [{\n" + " \"citycode\": \"0393\",\n" + " \"adcode\": \"410923\",\n" + " \"name\": \"福坎镇\",\n" + " \"center\": \"115.398,36.0869\",\n" + " \"level\": \"street\",\n" + " \"districts\": []\n" + " }]\n" + " }]\n" + " }]\n" + " }]\n" + " }]\n" + "}";//result.getcontent();*/ jsonobject jsonobject = jsonobject.parseobject(jsonstring); //获取到国家及下面所有的信息 开始循环插入,这里可以写成递归调用,但是不如这样方便查看、理解 jsonarray countryall = jsonobject.getjsonarray("districts"); for (int i = 0; i < countryall.size(); i++) { jsonobject countryleve0 = countryall.getjsonobject(i); string citycode0 = countryleve0.getstring("citycode"); string adcode0 = countryleve0.getstring("adcode"); string name0 = countryleve0.getstring("name"); string center0 = countryleve0.getstring("center"); string country = countryleve0.getstring("level"); int level = 0; if (country.equals("country")) { level = 0; } //插入国家 integer id1 = insert(0, adcode0, citycode0, name0, center0, level, name0); jsonarray province0 = countryleve0.getjsonarray("districts"); for (int j = 0; j < province0.size(); j++) { jsonobject province1 = province0.getjsonobject(j); string citycode1 = province1.getstring("citycode"); string adcode1 = province1.getstring("adcode"); string name1 = province1.getstring("name"); string center1 = province1.getstring("center"); string province = province1.getstring("level"); int level1 = 0; if (province.equals("province")) { level1 = 1; } //插入省 integer id2 = insert(id1, adcode1, citycode1, name1, center1, level1, name0, name1); jsonarray city0 = province1.getjsonarray("districts"); for (int z = 0; z < city0.size(); z++) { jsonobject city2 = city0.getjsonobject(z); string citycode2 = city2.getstring("citycode"); string adcode2 = city2.getstring("adcode"); string name2 = city2.getstring("name"); string center2 = city2.getstring("center"); string city = city2.getstring("level"); int level2 = 0; if (city.equals("city")) { level2 = 2; } //插入市 integer id3 = insert(id2, adcode2, citycode2, name2, center2, level2, name0, name1, name2); jsonarray street0 = city2.getjsonarray("districts"); for (int w = 0; w < street0.size(); w++) { jsonobject street3 = street0.getjsonobject(w); string citycode3 = street3.getstring("citycode"); string adcode3 = street3.getstring("adcode"); string name3 = street3.getstring("name"); string center3 = street3.getstring("center"); string street = street3.getstring("level"); int level3 = 0; if (street.equals("street")) { level3 = 2; } //插入区县 insert(id3, adcode3, citycode3, name3, center3, level3, name0, name1, name2, name3); // jsonarray street = street3.getjsonarray("districts"); //有需要可以继续向下遍历 } } } } return getsuccessresult(); } /** * 公共的插入方法 * * @param parentid 父id * @param citycode 城市编码 * @param adcode 区域编码 街道没有独有的adcode,均继承父类(区县)的adcode * @param name 城市名称 (行政区名称) * @param center 地理坐标 * @param level 等级 0 国家,1省,...依次排 * @param mergename 把名称拼接成全名 * @return */ public integer insert(integer parentid, string citycode, string adcode, string name, string center, int level, string... mergename) { // \"citycode\": [],\n" + // " \"adcode\": \"100000\",\n" + // " \"name\": \"*\",\n" + // " \"center\": \"116.3683244,39.915085\",\n" + // " \"level\": \"country\",\n" + regionamap record = new regionamap(); if (!adcode.equals("[]")) { record.setadcode(adcode); } record.setcitycode(integer.parseint(citycode)); record.setcenter(center); record.setlevel(level); record.setname(name); record.setparentid(parentid); string megname = ""; for (int i = 0; i < mergename.length; i++) { megname = megname + mergename[i]; if (i < mergename.length - 1) { megname = megname + ","; } } record.setmergername(megname); regionamapmappercustom.insertselective(record); return record.getcitycode(); } /** * 生成的sql顺序重新排序 * * @return */ @requestmapping("/order") public object order() { regionamapexample example = new regionamapexample(); example.setorderbyclause("citycode, adcode"); list<regionamap> selectbyexample = regionamapmappercustom.selectbyexample(example); for (regionamap regionamap : selectbyexample) { regionorder record = new regionorder(); record.setadcode(regionamap.getadcode()); record.setcitycode(regionamap.getcitycode()); record.setcenter(regionamap.getcenter()); record.setlevel(regionamap.getlevel()); record.setmergername(regionamap.getmergername()); record.setname(regionamap.getname()); record.setparentid(regionamap.getparentid()); regionordermapper.insertselective(record); } return getsuccessresult(); }
注意:第一次调用接口解析的省市区不是按顺序解析,需要调用排序接口重新生成。最后看排序后的 region_order 表就可以
上一篇: 站着说话不腰疼的一般都是胖子