使用高德地图api导入行政区域及经纬度
高德地图提供了行政区域及经纬度查询api
参考如下链接
具体实现代码如下
package com.test;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
import jvc.util.DBUtils;
import jvc.util.NetUtils;
import jvc.util.db.Insert;
public class Test {
static Map<String, String> levelMap = new HashMap<String, String>(){{//高德地图的省市区级别
this.put("province", "1");
this.put("city", "2");
this.put("district", "3");
this.put("biz_area", "4");
}};
public static void main(String[] args) throws Exception {
String str = NetUtils.getHtmlSource("http://restapi.amap.com/v3/config/district?keywords=新沟&level=district&subdistrict=3&key=053a406aad6f5afd49afd82a64a4e034",6000000,"utf-8");
JSONObject json = new JSONObject(str) ;
JSONArray ja = null;
judge(json.getJSONArray("districts"),"0");
}
public static void judge(JSONArray ja,String purl) throws Exception{
for(int i = 0 ; i <ja.length();i++){
//数据库存入操作
String id = insert(ja.getJSONObject(i), purl);
//判断是否递归
JSONArray cja = ja.getJSONObject(i).getJSONArray("districts");
if(cja.length() > 0)
judge(cja, id);
}
}
/**
*
* @param json 要存入的地区
* @param purl 上级区域
* @return
* @throws Exception
*/
public static String insert(JSONObject json,String purl) throws Exception{
Insert insert = new Insert("ED_CusStation");
String[] center = json.getString("center").split(",");
String id = DBUtils.getSerialNo("ED_CusStation")+"";
insert.setValue("longitude", center[0]);
insert.setValue("latitude", center[1]);
insert.setValue("sename", json.getString("name"));
insert.setValue("sename", json.getString("name"));
insert.setValue("purl",purl );
insert.setValue("locationStationId", id);
insert.setValue("level", levelMap.get(json.get("level")));
insert.execute();
return id;
}
}