前言: 最近項目中用到了全國省市區三級信息,但是網上找到的信息都是比較舊的信息。與現在最新的地區信息匹配不上。後來想到高德地圖上可能有這些信息。所以解析了一下api介面,生成了相關省市區的sql信息。(註意:這裡面沒有港臺地區)具體的請參看高德地圖文檔。 alibaba.fastjson解析+mys ...
前言:
最近項目中用到了全國省市區三級信息,但是網上找到的信息都是比較舊的信息。與現在最新的地區信息匹配不上。後來想到高德地圖上可能有這些信息。所以解析了一下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"); 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 表就可以