前言:
最近项目中用到了全国省市区三级信息,但是网上找到的信息都是比较旧的信息。与现在最新的地区信息匹配不上。后来想到高德地图上可能有这些信息。所以解析了一下api接口,生成了相关省市区的sql信息。(注意:这里面没有港台地区)具体的请参看高德地图文档。
alibaba.fastjson解析+mysql表
1、高德地图api调用接口说明文档:https://lbs.amap.com/api/webservice/guide/api/district/#limit
具体数据接口:
https://restapi.amap.com/v3/config/district?subdistrict=4&key=bb4198a1f146184af53322d424732f6b
注意:如果想调用此接口需要申请一个key,免费的,注册就可以获取
2、我的码云代码:https://gitee.com/liran123/yft-evidence
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"); ListselectByExample = 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 表就可以