热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

2022年实时最新省市区县乡镇街道geojson行政边界数据获取方法

geojson数据下载地址:https:hxkj.vipdemoechartsMap可下载的数据包含省级geojson行政边界数据、市级geojson行政边界数据、区县级geojson行政边界数据、省市区县街道行政编码四级联动数据(可精确到乡镇街道级)一、通过

geojson 数据下载地址:https://hxkj.vip/demo/echartsMap/

可下载的数据包含省级geojson行政边界数据、市级geojson行政边界数据、区/县级geojson行政边界数据、省市区县街道行政编码四级联动数据(可精确到乡镇/街道级)

一、通过API接口,实时获取最新中国省市区县geoJSON格式地图数据,可用于Echarts地图展示

1、效果图如下
在这里插入图片描述
在这里插入图片描述
2、示例代码

 downloadMapCode() {// 下载mapCode数据
      let mapCode = [], cityMapCode = [], provinceMapCode = [], provinceList = [], cityList = [],
        districtList = [];
 
      provinceList = this.codeList.filter(item => {
        return item.level === 'province'
      })
      cityList = this.codeList.filter(item => {
        return item.level === 'city'
      })
      districtList = this.codeList.filter(item => {
        return item.level === 'district'
      })
 
      districtList.forEach(item => {
        mapCode.push({
          name: item.name,
          cityCode: item.code,
          fatherCode: `${item.code.substring(0, 4)}00`,
          children: []
        })
      })
 
      // 筛选出直辖市下面的区县
      let direct = mapCode.filter(item => {
        return item.fatherCode.includes('0000');
      })
 
      for (let i in cityList) {
        let children = []
        for (let j in mapCode) {
          if (mapCode[j].fatherCode == cityList[i].code) {
            children.push(mapCode[j])
          }
        }
        cityMapCode.push({
          name: cityList[i].name,
          cityCode: cityList[i].code,
          fatherCode: `${cityList[i].code.substring(0, 2)}0000`,
          children: children
        })
      }
      cityMapCode = cityMapCode.concat(direct);
 
      for (let i in provinceList) {
        let children = []
        for (let j in cityMapCode) {
          if (cityMapCode[j].fatherCode == provinceList[i].code) {
            children.push(cityMapCode[j])
          }
        }
        provinceMapCode.push({
          name: provinceList[i].name,
          cityCode: provinceList[i].code,
          fatherCode: '100000',
          children: children
        })
      }
 
      if (provinceMapCode.length === 0) return
      this.zip.file(`mapCode.json`, JSON.stringify(provinceMapCode));
      this.downloadTips = '文件打包压缩中...';
      this.zip.generateAsync({ type: "blob" })
        .then((content) => {
          saveAs(content, "mapCode.zip");
        });
    },
    // 下载全国地名和编码(不包含边界数据)
    downloadNameAndCode() {
      let opts = {
        subdistrict: 3, //返回下一级行政区
        showbiz: false, //最后一级返回街道信息
      };
      let district = new AMap.DistrictSearch(opts); //注意:需要使用插件同步下发功能才能这样直接使用
      district.search('中国', function (status, result) {
        if (status === 'complete') {
          getData(result.districtList[0]);
        }
      });
      let _this = this
      function getData(data) {
        let districtList = data.districtList;
 
        let blob = new Blob([JSON.stringify(districtList)], {
          type: 'text/plain;charset=utf-8',
        });
        let filename = '全国省市区县街道和编码(不包含边界数据)';
        _this.$ba.trackEvent('echartsMap', '全国省市区县街道和编码(不包含边界数据)下载', filename);
        saveAs(blob, `${filename}.json`); //filename
      }
    },
    echartsMapClick(params) {//地图点击事件
      this.$ba.trackEvent('echartsMap', '点击地图', `${params.data.name}-${params.data.cityCode}`);
      if (params.data.level == 'street') return;
      //清除地图上所有覆盖物
      for (var i = 0, l = this.polygons.length; i  {
        if (status === 'complete') {
          this.getData(result.districtList[0], params.data.level, this.cityCode);
        }
      });
    },
    loadMapData(areaCode) {
      AMapUI.loadUI(['geo/DistrictExplorer'], DistrictExplorer => {
 
        //创建一个实例
        var districtExplorer = window.districtExplorer = new DistrictExplorer({
          eventSupport: true, //打开事件支持
          map: this.map
        });
 
        districtExplorer.loadAreaNode(areaCode, (error, areaNode) => {
 
          if (error) {
            console.error(error);
            return;
          }
          let mapJson = {};
          mapJson.type = "FeatureCollection";
          mapJson.features = areaNode.getSubFeatures();
          this.loadMap(this.cityName, mapJson);
          this.geoJsOnData= mapJson;
        });
      });
    },

二、通过获取到的数据整理一系列联动数据,实现了每天自动更新

1、效果图
在这里插入图片描述
2、示例代码

downloadJson(nameType) {//geo文件下载
      this.nameType = nameType
      if (nameType === 'area') {
        this.$ba.trackEvent('echartsMap', '文件下载', '下载级联数据');
        this.$refs.dialog.show();
        return;
      }
      if (nameType === 'all') {
        this.$ba.trackEvent('echartsMap', '文件下载', '打包下载全部');
        this.$refs.dialog.show();
        return;
      }
      if (nameType === 'street') {
        this.$ba.trackEvent('echartsMap', '文件下载', '下载乡镇数据');
        this.$refs.streetDialog.show();
        return;
      }
      var blob = new Blob([JSON.stringify(this.geoJsonData)], { type: "text/plain;charset=utf-8" });
      let filename = this.cityName;
      if (nameType === 'code') {
        filename = this.cityCode;
      }
      this.$ba.trackEvent('echartsMap', '文件下载', filename);
      saveAs(blob, `${filename}.geoJson`);//filename
    },
    dialogConfirm() {
      if (this.nameType === 'area') {
        this.$refs.mapDataDialog.show();
      } else {
        this.downloadAllJson()
      }
    },
    downloadAllJson() {//一次打包下载所有的数据
      this.showTips();
      if (this.downloadTips != '下载geoJson数据') {
        return;
      }
      this.codeList = [];
 
      this.downloadTips = '获取数据中...';
 
      //                this.district.setLevel('country'); //行政区级别
      this.district.setExtensions('all');
      console.log('开始递归循环获取地区code..');
      this.loopSearch('中国');
 
    },
    loopSearch(code) {
      setTimeout(() => {
        this.district.search(code, (status, result) => {
          if (status == 'complete') {
            console.log(`${code}--获取成功`)
            for (let i in result.districtList[0].districtList) {
              this.codeList.push({
                name: result.districtList[0].districtList[i].name,
                code: result.districtList[0].districtList[i].adcode,
                level: result.districtList[0].districtList[i].level
              })
              //这边没想出来怎么判断数据是否全部加载完毕了,只能采用这种死办法
              //有更好解决方案的大佬,麻烦告诉我一下,邮箱t@tsy6.com
              //或者直接Github提交PR,在此不胜感激
              if (this.codeList.length >= 428) {// 为 3718 时,获取区县数据,428 省市数据
                console.log('code获取完成');
                this.isCodeListLoadComplete = true;
              }
              if (result.districtList[0].districtList[i].adcode && result.districtList[0].districtList[i].level != 'city' && result.districtList[0].districtList[i].level != 'district' && result.districtList[0].districtList[i].level != 'street') {
                this.loopSearch(result.districtList[0].districtList[i].adcode)
              }
            }
          } else {//第一遍查询出错,再次执行查询
            console.log(`${code}--第一次获取失败,正在尝试进行第二次获取`)
            this.district.search(code, (status, result) => {
              if (status == 'complete') {
                console.log(`${code}--第二次获取成功`)
                for (let i in result.districtList[0].districtList) {
                  this.codeList.push({
                    name: result.districtList[0].districtList[i].name,
                    code: result.districtList[0].districtList[i].adcode,
                    level: result.districtList[0].districtList[i].level
                  })
                  //这边没想出来怎么判断数据是否全部加载完毕了,只能采用这种死办法
                  //有更好解决方案的大佬,麻烦告诉我一下,邮箱t@tsy6.com
                  //或者直接Github提交PR,在此不胜感激
                  if (this.codeList.length >= 428) {
                    console.log('code获取完成');
                    this.isCodeListLoadComplete = true;
                  }
                }
              } else {
                console.log(`${code}--第二次获取失败,请联系email:t@tsy6.com`)
              }
            })
          }
        });
      }, 500)
    },
    loadAllGeoJson() {//通过codeList加载全部geoJson数据
      console.log('开始加载geoJson数据');
      AMapUI.loadUI(['geo/DistrictExplorer'], DistrictExplorer => {
 
        //创建一个实例
        var districtExplorer = window.districtExplorer = new DistrictExplorer({
          eventSupport: true, //打开事件支持
          map: this.map
        });
        let mapJson = {};
        for (let i in this.codeList) {
          setTimeout(() => {
            districtExplorer.loadAreaNode(this.codeList[i].code, (error, areaNode) => {
              if (error) {
                this.codeList[i].geo = 'error';
                console.log(`${this.codeList[i].name}--${this.codeList[i].code},geo 数据获取失败,高德地图的锅^_^`)
              } else {
                mapJson.type = "FeatureCollection";
                mapJson.features = areaNode && areaNode.getSubFeatures() || '';
                this.codeList[i].geo = mapJson;
                console.log(`${this.codeList[i].level}--${this.codeList[i].name}--${this.codeList[i].code},geo 数据获取成功,马上为你打包`)
              }
 
              if (this.codeList[i].level === 'province') {
                this.zip.file(`100000/${this.codeList[i].code}.geoJson`, JSON.stringify(mapJson));
              } else {
                this.zip.file(`100000/${this.codeList[i].code.substring(0, 2)}0000/${this.codeList[i].code}.geoJson`, JSON.stringify(mapJson));
              }
 
              if (this.codeList.every(item => item.geo)) {
                console.log('ziped');
                let readme = `\r\n
                                            项目源码github地址:https://github.com/TangSY/echarts-map-demo (欢迎star)
                                            \r\n
                                            个人空间:https://www.hxkj.vip (欢迎闲逛)
                                            \r\n
                                             Email:t@tsy6.com  (遇到问题可以反馈)
                                         `;
                this.zip.file(`readMe(sourceCode).txt`, readme);
                this.downloadTips = '文件打包压缩中...';
                this.zip.generateAsync({ type: "blob" })
                  .then((content) => {
                    saveAs(content, "geoJson数据包.zip");
                    this.downloadTips = '下载geoJson数据';
                    this.isCodeListLoadComplete = false;
                    this.$ba.trackEvent('echartsMap', '文件下载', '打包下载成功');
                  });
              }
            });
          }, 100 * i)
        }
      });
    },

推荐阅读
  • 本文介绍了使用PHP实现断点续传乱序合并文件的方法和源码。由于网络原因,文件需要分割成多个部分发送,因此无法按顺序接收。文章中提供了merge2.php的源码,通过使用shuffle函数打乱文件读取顺序,实现了乱序合并文件的功能。同时,还介绍了filesize、glob、unlink、fopen等相关函数的使用。阅读本文可以了解如何使用PHP实现断点续传乱序合并文件的具体步骤。 ... [详细]
  • 本文介绍了在Windows环境下如何配置php+apache环境,包括下载php7和apache2.4、安装vc2015运行时环境、启动php7和apache2.4等步骤。希望对需要搭建php7环境的读者有一定的参考价值。摘要长度为169字。 ... [详细]
  • 本文讨论了在手机移动端如何使用HTML5和JavaScript实现视频上传并压缩视频质量,或者降低手机摄像头拍摄质量的问题。作者指出HTML5和JavaScript无法直接压缩视频,只能通过将视频传送到服务器端由后端进行压缩。对于控制相机拍摄质量,只有使用JAVA编写Android客户端才能实现压缩。此外,作者还解释了在交作业时使用zip格式压缩包导致CSS文件和图片音乐丢失的原因,并提供了解决方法。最后,作者还介绍了一个用于处理图片的类,可以实现图片剪裁处理和生成缩略图的功能。 ... [详细]
  • 本文介绍了OkHttp3的基本使用和特性,包括支持HTTP/2、连接池、GZIP压缩、缓存等功能。同时还提到了OkHttp3的适用平台和源码阅读计划。文章还介绍了OkHttp3的请求/响应API的设计和使用方式,包括阻塞式的同步请求和带回调的异步请求。 ... [详细]
  • 本文介绍了Python高级网络编程及TCP/IP协议簇的OSI七层模型。首先简单介绍了七层模型的各层及其封装解封装过程。然后讨论了程序开发中涉及到的网络通信内容,主要包括TCP协议、UDP协议和IPV4协议。最后还介绍了socket编程、聊天socket实现、远程执行命令、上传文件、socketserver及其源码分析等相关内容。 ... [详细]
  • 本文介绍了在Python3中如何使用选择文件对话框的格式打开和保存图片的方法。通过使用tkinter库中的filedialog模块的asksaveasfilename和askopenfilename函数,可以方便地选择要打开或保存的图片文件,并进行相关操作。具体的代码示例和操作步骤也被提供。 ... [详细]
  • Voicewo在线语音识别转换jQuery插件的特点和示例
    本文介绍了一款名为Voicewo的在线语音识别转换jQuery插件,该插件具有快速、架构、风格、扩展和兼容等特点,适合在互联网应用中使用。同时还提供了一个快速示例供开发人员参考。 ... [详细]
  • Redis底层数据结构之压缩列表的介绍及实现原理
    本文介绍了Redis底层数据结构之压缩列表的概念、实现原理以及使用场景。压缩列表是Redis为了节约内存而开发的一种顺序数据结构,由特殊编码的连续内存块组成。文章详细解释了压缩列表的构成和各个属性的含义,以及如何通过指针来计算表尾节点的地址。压缩列表适用于列表键和哈希键中只包含少量小整数值和短字符串的情况。通过使用压缩列表,可以有效减少内存占用,提升Redis的性能。 ... [详细]
  • 第四章高阶函数(参数传递、高阶函数、lambda表达式)(python进阶)的讲解和应用
    本文主要讲解了第四章高阶函数(参数传递、高阶函数、lambda表达式)的相关知识,包括函数参数传递机制和赋值机制、引用传递的概念和应用、默认参数的定义和使用等内容。同时介绍了高阶函数和lambda表达式的概念,并给出了一些实例代码进行演示。对于想要进一步提升python编程能力的读者来说,本文将是一个不错的学习资料。 ... [详细]
  • 企业数据应用挑战及元数据管理的重要性
    本文主要介绍了企业在日常经营管理过程中面临的数据应用挑战,包括数据找不到、数据读不懂、数据不可信等问题。针对这些挑战,通过元数据管理可以实现数据的可见、可懂、可用,帮助业务快速获取所需数据。文章提出了“灵魂”三问——元数据是什么、有什么用、又该怎么管,强调了元数据管理在企业数据治理中的基础和前提作用。 ... [详细]
  • 本文介绍了前端人员必须知道的三个问题,即前端都做哪些事、前端都需要哪些技术,以及前端的发展阶段。初级阶段包括HTML、CSS、JavaScript和jQuery的基础知识。进阶阶段涵盖了面向对象编程、响应式设计、Ajax、HTML5等新兴技术。高级阶段包括架构基础、模块化开发、预编译和前沿规范等内容。此外,还介绍了一些后端服务,如Node.js。 ... [详细]
  • Python使用Pillow包生成验证码图片的方法
    本文介绍了使用Python中的Pillow包生成验证码图片的方法。通过随机生成数字和符号,并添加干扰象素,生成一幅验证码图片。需要配置好Python环境,并安装Pillow库。代码实现包括导入Pillow包和随机模块,定义随机生成字母、数字和字体颜色的函数。 ... [详细]
  • 本文整理了315道Python基础题目及答案,帮助读者检验学习成果。文章介绍了学习Python的途径、Python与其他编程语言的对比、解释型和编译型编程语言的简述、Python解释器的种类和特点、位和字节的关系、以及至少5个PEP8规范。对于想要检验自己学习成果的读者,这些题目将是一个不错的选择。请注意,答案在视频中,本文不提供答案。 ... [详细]
  • 简述在某个项目中需要分析PHP代码,分离出对应的函数调用(以及源代码对应的位置)。虽然这使用正则也可以实现,但无论从效率还是代码复杂度方面考虑ÿ ... [详细]
  • 本文介绍了如何使用OpenXML按页码访问文档内容,以及在处理分页符和XML元素时的一些挑战。同时,还讨论了基于页面的引用框架的局限性和超越基于页面的引用框架的方法。最后,给出了一个使用C#的示例代码来按页码访问OpenXML内容的方法。 ... [详细]
author-avatar
手机用户2602938875
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有