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

二、通过获取到的数据整理一系列联动数据,实现了每天自动更新【2022年实时最新省市区县乡镇街道geojson行政边界数据获取方法】1、效果图

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

文章插图
2、示例代码
downloadJson(nameType) {//geo文件下载this.nameType = nameTypeif (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\nEmail: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)}});},

经验总结扩展阅读