echart全国地图JSON文件下载工具类

目前基本上没有找到可以直接下载最新地图数据的,都是以前下载好的数据包,如果有地图数据有更新那就凉凉,经过一番摸索直接借鉴网络上的文件下载工具类弄的数据下载工具, 数据源是阿里巴巴提供的

http://datav.aliyun.com/tools/atlas/#&lat=31.769817845138945&lng=104.29901249999999&zoom=4

 

工具类的源地址是   http://blog.csdn.net/xb12369/article/details/40543649/

有兴趣的可以去看看, 不过这个数据只能一省份一个省份下载 需要用的时候直接运行我的工具类就好了!!

 

package org.cboard.controller;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Java读取并下载网络文件
 * @author Marydon
 * @createTime 2017年8月18日下午5:24:09
 * @updateTime
 * @Email:marydon2017@163.com
 * @version:1.0.0
 * @referenceLink
 *  <a href="http://blog.csdn.net/xb12369/article/details/40543649/"> java 从网络Url中下载文件</a>
 */
public class DownLoadFile {
    /**
     * 从网络Url中下载文件
     * @param urlStr
     * @param fileName
     * @param savePath
     * @throws IOException
     */
    public static void  downLoadFromUrl(String urlStr,String fileName,String savePath) throws IOException{
        URL url = new URL(urlStr);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        //设置超时间为3秒
        conn.setConnectTimeout(3*1000);
        //防止屏蔽程序抓取而返回403错误
        conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

        //得到输入流
        InputStream inputStream = conn.getInputStream();
        //获取自己数组
        byte[] getData = readInputStream(inputStream);

        //文件保存位置
        File saveDir = new File(savePath);
        if(!saveDir.exists()){
            saveDir.mkdirs();
        }
        File file = new File(saveDir+File.separator+fileName);
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(getData);
        if(fos!=null){
            fos.close();
        }
        if(inputStream!=null){
            inputStream.close();
        }

        System.out.println("info:"+url+" download success");

    }

    /**
     * 从输入流中获取字节数组
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static  byte[] readInputStream(InputStream inputStream) throws IOException {
        byte[] buffer = new byte[1024];
        int len = 0;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while((len = inputStream.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        bos.close();
        return bos.toByteArray();
    }

    public static void main(String[] args) {
        try{
            String [] provinceIds={"110000", "120000", "130000", "140000", "150000", "210000", "220000", "230000", "310000", "320000", "330000", "340000", "350000", "360000", "370000", "410000", "420000", "430000", "440000", "450000", "460000", "500000", "510000", "520000", "530000", "540000", "610000", "620000", "630000", "640000", "650000", "710000", "810000", "820000", "100000"};
            String [] cityIds={"110101","110102","110105","110106","110107","110108","110109","110111","110112","110113","110114","110115","110116","110117","110118","110119","120101","120102","120103","120104","120105","120106","120110","120111","120112","120113","120114","120115","120116","120117","120118","120119","130100","130200","130300","130400","130500","130600","130700","130800","130900","131000","131100","140100","140200","140300","140400","140500","140600","140700","140800","140900","141000","141100","150100","150200","150300","150400","150500","150600","150700","150800","150900","152200","152500","152900","210100","210200","210300","210400","210500","210600","210700","210800","210900","211000","211100","211200","211300","211400","220100","220200","220300","220400","220500","220600","220700","220800","222400","230100","230200","230300","230400","230500","230600","230700","230800","230900","231000","231100","231200","232700","310101","310104","310105","310106","310107","310109","310110","310112","310113","310114","310115","310116","310117","310118","310120","310151","320100","320200","320300","320400","320500","320600","320700","320800","320900","321000","321100","321200","321300","330100","330200","330300","330400","330500","330600","330700","330800","330900","331000","331100","340100","340200","340300","340400","340500","340600","340700","340800","341000","341100","341200","341300","341500","341600","341700","341800","350100","350200","350300","350400","350500","350600","350700","350800","350900","360100","360200","360300","360400","360500","360600","360700","360800","360900","361000","361100","370100","370200","370300","370400","370500","370600","370700","370800","370900","371000","371100","371300","371400","371500","371600","371700","420100","420200","420300","420500","420600","420700","420800","420900","421000","421100","421200","421300","422800","429004","429005","429006","429021","410100","410200","410300","410400","410500","410600","410700","410800","410900","411000","411100","411200","411300","411400","411500","411600","411700","419001","430100","430200","430300","430400","430500","430600","430700","430800","430900","431000","431100","431200","431300","433100","460100","460200","460300","460400","469001","469002","469005","469006","469007","469021","469022","469023","469024","469025","469026","469027","469028","469029","469030","440100","440200","440300","440400","440500","440600","440700","440800","440900","441200","441300","441400","441500","441600","441700","441800","441900","442000","442100","445100","445200","445300","450100","450200","450300","450400","450500","450600","450700","450800","450900","451000","451100","451200","451300","451400","500101","500102","500103","500104","500105","500106","500107","500108","500109","500110","500111","500112","500113","500114","500115","500116","500117","500118","500119","500120","500151","500152","500153","500154","500155","500156","500229","500230","500231","500233","500235","500236","500237","500238","500240","500241","500242","500243","510100","510300","510400","510500","510600","510700","510800","510900","511000","511100","511300","511400","511500","511600","511700","511800","511900","512000","513200","513300","513400","520100","520200","520300","520400","520500","520600","522300","522600","522700","530100","530300","530400","530500","530600","530700","530800","530900","532300","532500","532600","532800","532900","533100","533300","533400","610100","610200","610300","610400","610500","610600","610700","610800","610900","611000","540100","540200","540300","540400","540500","540600","542500","620100","620200","620300","620400","620500","620600","620700","620800","620900","621000","621100","621200","622900","623000","640100","640200","640300","640400","640500","630100","630200","632200","632300","632500","632600","632700","632800","710000","650100","650200","650400","650500","652300","652700","652800","652900","653000","653100","653200","654000","654200","654300","659001","659002","659003","659004","659005","659006","659007","659008","659009","820001","820002","820003","820004","820005","820006","820007","820008","810001","810002","810003","810004","810005","810006","810007","810008","810009","810010","810011","810012","810013","810014","810015","810016","810017","810018"};

            String fileUrl="https://geo.datav.aliyun.com/areas_v2/bound/";
            String downLoadFileUrl="";

            // 下载省份数据
            for(String provinceId:provinceIds){
                downLoadFileUrl=fileUrl+provinceId+"_full.json";
                downLoadFromUrl(downLoadFileUrl,provinceId + ".json","E:/Downloads/map/province");
            }

            //下载市级数据
            for(String fileId:cityIds){
                downLoadFileUrl=fileUrl+fileId+".json";
                downLoadFromUrl(downLoadFileUrl,fileId + ".json","E:/Downloads/map/citys");
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

最近在做echart+vue 的地图 我是动态加载 有人问上面的id是哪里来的  我是在vue里面用js处理的 这块也顺便贴上来  ,学艺不精

原本定义了城市cityids是个数组 不知道为啥这里push的时候打印有点问题  索性改成这样了  最后一次的数据复制就好 ,另外城市数据中包含了省份的id,暂时没有仔细研究 手动删除了一下 将就这用吧.

 

let mapJson = await import("../../map/china.json");
const provinceIds=[]
  let cityIds=''
  mapJson.features.forEach(item=>{
      provinceIds.push(item.properties.adcode)
  })
  provinceIds.forEach(provinceId=>{
      let provinceData = import(`../../map/province/${provinceId}.json`);
      provinceData.then(res => {
          res.features.forEach(prov=>{
              cityIds+='"'+prov.properties.adcode+'",'
              console.log(cityIds)
              // cityIds.push(prov.properties.adcode)
          })
      })
  })
  console.log(cityIds)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值