首页 > 技术文章 > 通过nodejs 下载阿里云省、市、区的json文件,使用在echart上

vs1435 2020-12-04 19:39 原文

echart 实现地图下钻,需要数据,可以使用阿里云提供的线上api,地址是:

  http://datav.aliyun.com/tools/atlas/#&lat=30.332329214580188&lng=106.72278672066881&zoom=3.5

 

如果想离线下载所有的json文件,可以通过脚本下载:

目录结构:

 

 

index.js

var https = require('https');
var fs = require('fs');

function writeFile(filename, strData) {
  var fd = fs.openSync('./data/' + filename + '.json', 'w+');
  fs.writeFileSync(fd, strData);
  fs.closeSync(fd);
}

function logLog() {
  Array.prototype.push.call(arguments, '\033[0m');
  Array.prototype.unshift.call(arguments, '\033[;32m =>');
  console.log.apply(this, arguments);
}

function logError() {
  Array.prototype.push.call(arguments, '\033[0m');
  Array.prototype.unshift.call(arguments, '\033[;31m =>');
  console.log.apply(this, arguments);
}

function httpsGet(url, cb) {
  if (!url) return cb({ message: "url为空" });
  https.get(url, function (res) {
    if (res.statusCode !== 200) return cb({ message: "状态不等于200!" });
    res.setTimeout(5000);
    res.setEncoding('utf8');
    var rawData = '';
    res.on('data', function (chunk) { rawData += chunk; });
    res.on('end', function () {
      cb(null, rawData);
    });
  }).on('error', function (e) {
    cb(e);
  });
}

function getJson(code, name, province) {
  province = province || 'province';

  httpsGet("https://geo.datav.aliyun.com/areas_v2/bound/" + code + "_full.json", function (err, data) {
    if (err) return logError(name + '_' + code);

    writeFile(code, data);
    logLog(code + '_' + name);

    var list = JSON.parse(data);
    list.features.forEach(function (item) {
      var properties = item.properties;
      var level = properties.level;
      var adcode = properties.adcode;
      var _name = properties.name;
      if (level === province && adcode !== code) {
        getJson(adcode, _name)
      }
    });
  })
}

getJson(100000, '全国', 'province')

// getJson(460000, '海南省', 'city')
// getJson(140000, '山西省', 'city')
// getJson(520000, '贵州省', 'city')
// getJson(320000, '江苏省', 'city')
// getJson(440000, '广东省', 'city')
// getJson(220000, '吉林省', 'city')
// getJson(510000, '四川省', 'city')
// getJson(530000, '云南省', 'city')
// getJson(210000, '辽宁省', 'city')
// getJson(360000, '江西省', 'city')
// getJson(370000, '山东省', 'city')
// getJson(430000, '湖南省', 'city')
// getJson(230000, '黑龙江省', 'city')
// getJson(610000, '陕西省', 'city')
// getJson(340000, '安徽省', 'city')
// getJson(620000, '甘肃省', 'city')
// getJson(410000, '河南省', 'city')
// getJson(630000, '青海省', 'city')
// getJson(420000, '湖北省', 'city')
// getJson(710000, '台湾省', 'city')
// getJson(130000, '河北省', 'city')
// getJson(330000, '浙江省', 'city')

// getJson(820000, '澳门', 'city')
// getJson(810000, '香港', 'city')
// getJson(310000, '上海市', 'city')
// getJson(110000, '北京市', 'city')
// getJson(120000, '天津市', 'city')
// getJson(500000, '重庆市', 'city')

// getJson(640000, '宁夏回族自治区', 'city')
// getJson(150000, '内蒙古自治区', 'city')
// getJson(450000, '广西壮族自治区', 'city')
// getJson(650000, '新疆维吾尔自治区', 'city')

 

执行脚本 node index.js:

 

 

 

 

下载成功后,data 目录会生成文件,以code命名:

 

 

如果需要下载省下面的市区,请将注释代码去掉

 

推荐阅读