高德地图API 城市编码对照表insert到数据库

原创
2016/12/05 15:18
阅读数 3.9K

Java代码: dept.json就是高德接口返回的完整json,我保存到了本地处理的。

public class LocationTest {

    public static void main(String[] args) throws IOException {
        initDb();
        //高德的接口描述页面
        //http://lbs.amap.com/api/webservice/guide/api/district/#district
        //接口地址
        //http://restapi.amap.com/v3/config/district?key=<你申请的webapi的key>&keywords=中国&subdistrict=3&showbiz=false&extensions=base
        File file = new File("C:\\Users\\tj\\Desktop\\dept.json");
        String s = FileUtils.readFileToString(file, "utf-8");
        JSONObject o = JSON.parseObject(s).getJSONArray("districts").getJSONObject(0);
        save(o, 0, ",0,",1);
    }

    static int id = 10000;
    public static int nextId(){
        id++;
        return id;
    }

    public static void save(JSONObject o,int pid,String ppath,int depth){
        String adcode = o.getString("adcode");
        String name = o.getString("name");
        String level = o.getString("level");
        String center = o.getString("center");
        String lat = center.split(",")[1];
        String lont = center.split(",")[0];
        Area area = new Area();
        area.set("adcode",adcode);
        String citycode = o.getString("citycode");
        if(!citycode.equals("[]")){
            area.set("citycode",citycode);
        }
        int id = nextId();
        area.set("id", id);
        area.set("name",name);
        area.set("level",level);
        area.set("lat",lat);
        area.set("lont",lont);
        area.set("adcode",adcode);
        area.set("spell", PinyinHelper.convertToPinyinString(name,"",PinyinFormat.WITHOUT_TONE));
        area.set("short_spell", PinyinHelper.getShortPinyin(name));
        area.set("pid",pid);
        String mypath = ppath+id+",";
        area.set("path",mypath);
        area.set("depth", depth);
        boolean b = true;
        try {
            b = area.save();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(!b) {
            System.out.println("保存失败:" + name+" adcode:"+id);
        }
        JSONArray array = o.getJSONArray("districts");
        for (int i = 0; i < array.size(); i++) {
            JSONObject so = array.getJSONObject(i);
            save(so, id, mypath,depth+1);
        }
    }

    /**
     * 初始化数据库
     */
    public static void initDb(){
        String jdbcUrl = "jdbc:mysql://localhost:3306/wd_qx?characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull";
        DruidPlugin druidPlugin = new DruidPlugin(jdbcUrl, "root", "password","com.mysql.jdbc.Driver");
        druidPlugin.addFilter(new WallFilter());
        druidPlugin.addFilter(new StatFilter());
        druidPlugin.start();
        ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
        arp.addMapping("ts_area_tj", "id", Area.class);
        arp.setDialect(new MysqlDialect());
        arp.start();
    }
}

表结构:

CREATE TABLE `ts_area` (
  `id` varchar(15) NOT NULL,
  `name` varchar(60) DEFAULT NULL,
  `adcode` varchar(20) DEFAULT NULL,
  `citycode` varchar(20) DEFAULT NULL,
  `pid` varchar(20) DEFAULT NULL,
  `level` varchar(20) DEFAULT NULL,
  `lat` double DEFAULT NULL,
  `lont` double DEFAULT NULL,
  `spell` varchar(100) DEFAULT NULL,
  `short_spell` varchar(200) DEFAULT NULL,
  `path` varchar(1000) DEFAULT NULL,
  `depth` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

依赖的jar:

    

上传不了附件,sql没办法传上来了。

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部