利用高德api获取行政区边界geoJson,基于echart实现全国各省地磁分布统计图

利用高德api获取行政区边界geoJson,基于echart实现全国各省地磁分布统计图

前提条件:
每次涉及到全国地图的echart统计图表时,都要费时间去找、去下载一份离线的中国地图json数据,有点麻烦呢,此方法可以利用高德地图api,在线获取一份中国地图数据,很方便吖!

效果如图:
在这里插入图片描述
代码如下:
(1)使用npm方式安装Loader
npm i @amap/amap-jsapi-loader --save

(2)新建页面,并在页面中引入amap-jsapi-loader,初始化地图数据,继而进一步进行其他逻辑的处理

<template>
    <div>
        <div id="ZGDT3" class="w-100 h-100"></div>
    </div>
</template>

<script>
import AMapLoader from '@amap/amap-jsapi-loader';
import displayYNDCService from "@/services/displayYNDC";
export default {
    components: {},
    data() {
        return {
            mapKeyValue:"aa31c5bXXXXXX", //此处为自己申请的高德地图密钥key
            geoJsonZGDT3: {
                features: []
            },
            listZGDT3:[],
        }
    },
    mounted() {
        this.getGeoJson();
    },
    methods: {
        //利用高德api获取行政区边界geoJson,areaCode为区域编码
        getGeoJson() {
            let that = this;
            let areaCode = 100000; //中国

            AMapLoader.reset();
            
            AMapLoader.load({
                    key:that.mapKeyValue,    // 申请好的Web端开发者Key,首次调用 load 时必填
                    version:"2.0",           // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
                    plugins:[],              // 需要使用的的插件列表,如比例尺'AMap.Scale'等
                    AMapUI: {                // 是否加载 AMapUI,缺省不加载
                        version: '1.1',      // AMapUI 版本
                        plugins:['geo/DistrictExplorer'],   // 需要加载的 AMapUI ui插件
                    },
                }).then((AMap)=>{

                    let districtExplorer = new AMapUI.DistrictExplorer();

                    districtExplorer.loadAreaNode(areaCode, function (error, areaNode) {
                        if (error) {
                            console.error(error);
                            return;
                        }
                        
                        //获取当前所选中区域下的所有子级区域
                        let childAreaJson = areaNode.getSubFeatures();

                        that.geoJsonZGDT3.features = childAreaJson;
                    
                        that.getData(areaCode);
                    });

                }).catch(e=>{
                    console.log(e);
                })
        },

        //获取地磁分布统计数据
        async getData() {
            
            this.listZGDT3 = [];

            let dataObj = await displayYNDCService.getData();

            let areaList = this.geoJsonZGDT3.features; //区域列表
            let areaDataList = dataObj.DeviceByAddress; //区域数据列表
            
            let mapDataList = []; //地图数据列表

            //初始化地图数据列表
            areaList.forEach(item => {
                let mapDataObj0 = {
                    name: item.properties.name,
                    value: [item.properties.center[0], item.properties.center[1], -1],
                    areaCode: item.properties.adcode,
                    level:item.properties.level
                }
                mapDataList.push(mapDataObj0);
            });

            //对比区域列表与区域数据列表,找到code相等的数据,将地图数据列表中对应位置上的数据替换掉
            areaList.forEach((item0,index0) => {
                areaDataList.forEach(item1 => {
                    if(item0.properties.adcode == item1.addressCode){
                        let mapDataObj1 = {
                            name: item0.properties.name,
                            value: [item0.properties.center[0], item0.properties.center[1], item1.count],
                            areaCode: item0.properties.adcode,
                            level:item0.properties.level
                        }
                        mapDataList[index0] = mapDataObj1;
                    }
                })
            });

            //将地图数据列表进行排序
            mapDataList = mapDataList.sort(function (a, b) {
                return b.value[2] - a.value[2];
            });
            
            this.listZGDT3 = mapDataList;
        },

        //初始化中国地图
        initChartZGDT3(){
            // 解决echarts多次渲染问题.如果已渲染则销毁
            if (this.chartZGDT3) this.chartZGDT3.dispose();

            // 基于准备好的dom,初始化echarts实例
            let chartZGDT3 = (this.chartZGDT3 = this.$echarts.init(document.getElementById("ZGDT3")));

            //注册地图
            this.$echarts.registerMap('china', this.geoJsonZGDT3);

            let mapDataList = this.listZGDT3;
            
            let max = 0;
            let mapDataList_noZero = [];

            if(mapDataList.length>0){
                //获取地图数据列表中的最大值
                max = mapDataList[0].value[2];
                
                //从地图数据列表中筛选出数量不为0的数据组成新的列表
                mapDataList_noZero = mapDataList.filter(item => item.value[2] !== -1);
            }
            
            let optionZGDT3 = this.optionZGDT3 = {
                tooltip: {
                    trigger: 'item',
                    backgroundColor: 'rgba(0, 0, 0, 0.8)',
                    borderWidth:1,
                    borderColor: '#46F0ED',
                    showDelay: 0,
                    hideDelay: 0,
                    enterable: true,
                    transitionDuration: 0,
                    extraCssText: 'z-index:100',
                    formatter: function(params) {
                        //根据业务自己拓展要显示的内容
                        let res = "";
                        let name = params.name;
                        let value = 0;
                        let type = params.seriesType;
                        if(type=="map"){
                            value = params.data.value[2]==-1?0:params.data.value[2];
                            res = "<span style='color:#fff;'>" + name + "</span><br/>数据:" + value;
                        }
                        else{
                            value = params.value[2]==-1?0:params.value[2];
                            res = "<span style='color:#fff;'>" + name + "</span><br/>数据:" + value;
                        }
                        return res;
                    },
                    textStyle: {
                        fontSize: 12
                    }
                },
                visualMap: { //图例值控制
                    left:'4%',
                    min: 0,
                    max: max,
                    itemWidth:12,
                    itemHeight:145,
                    seriesIndex: [1],
                    calculable: true,
                    show: true,
                    color: ['#f44336', '#fc9700', '#ffde00', '#ffde00', '#36E771'],
                    textStyle: {
                        color: '#fff'
                    }
                },
                geo: { //不可去除,因为他是与带有涟漪特效动画的散点(气泡)图绑定在一起的
                    map: 'china',
                    aspectScale: 0.83,
                    zoom: 1.7,
                    roam: false, //是否允许缩放
                    top:'28%',
                    // left:'30%',
                    label: {
                        normal: {
                            show: false,
                            textStyle:{
                                color: '#fff'
                            }
                        }
                    },
                    itemStyle: {
                        normal: {
                            borderColor: '#1AE0FB', //省市边界线
                            borderWidth: 0.8,
                            areaColor: {
                                type: 'linear-gradient',
                                x: 1000,
                                y: 600,
                                x2: 1000,
                                y2: 0,
                                colorStops: [{
                                    offset: 0,
                                    color: '#274d68' // 0% 处的颜色
                                }, {
                                    offset: 1,
                                    color: '#09132c' // 50% 处的颜色
                                }],
                                global: true // 缺省为 false
                            },
                        }
                    },
                    emphasis:{
                        label:{
                            show:false
                        },
                        itemStyle:{
                            areaColor:'rgba(10, 10, 10, 0.8)'
                        }
                    }
                },
                series:[
                    {
                        type: 'map',
                        mapType: 'china',
                        aspectScale: 0.83,
                        zoom: 1.7, //当前视角的缩放比例
                        roam: false, //是否开启平游或缩放
                        top:'28%',
                        // left:'30%',
                        scaleLimit: { //滚轮缩放的极限控制
                            min: 1,
                            max: 2
                        },
                        label: {
                            normal: {
                                show: false,
                                textStyle:{
                                    color: '#fff'
                                }
                            }
                        },
                        itemStyle: {
                            normal: {
                                borderColor: '#1AE0FB', //省市边界线
                                borderWidth: 0.8,
                                areaColor:"rgba(0,10,56,0.8)"
                            }
                        },
                        emphasis:{
                            label:{
                                show:false
                            },
                            itemStyle:{
                                areaColor:"rgba(10, 10, 10, 0.8)"
                            }
                        },
                        data: mapDataList
                    },
                    {
                        type: 'effectScatter',
                        coordinateSystem: 'geo',
                        rippleEffect: { //涟漪特效
                            period: 4, //动画时间,值越小速度越快
                            brushType: 'stroke', //波纹绘制方式 stroke, fill
                            scale: 4 //波纹圆环最大限制,值越大波纹越大
                        },
                        label: {
                            normal: {
                                show: true,
                                position: 'right', //显示位置
                                offset: [8, 0], //偏移设置
                                formatter: function(params){//圆环显示文字
                                    return params.data.name + " - " + params.data.value[2];
                                },
                                fontSize: 13,
                                
                                color:'#ffffff',
                                lineHeight:20,
                                backgroundColor:'rgba(0, 0, 0, 0.8)',
                                borderWidth:1,
                                borderColor: '#1AE0FB',
                                borderRadius:4,
                                padding:[7,18,4,7],
                            },
                            emphasis: {
                                show: false
                            }
                        },
                        symbol: 'circle',
                        symbolSize: function(val) {
                            //return 5+ val[2] * 5; //圆环大小
                            return 7;
                        },
                        itemStyle: {
                            normal: {
                                //show: false,
                                // color: '#f00'
                                show: true,
                                color: '#F39D13',
                                shadowBlur: 10,
                                shadowColor: '#F39D13'
                            },
                            emphasis: {
                                show: false,
                                color: 'rgba(37, 43, 61, .5)' //悬浮背景
                            }
                        },
                        data: mapDataList_noZero
                    },
                    //目标点
                    {
                        type: 'scatter',
                        coordinateSystem: 'geo',
                        zlevel: 4,
                        rippleEffect: {
                            period: 4,
                            brushType: 'stroke',
                            scale: 4
                        },
                        itemStyle:{
                            color:"#FA4E4B"
                        },
                        label: {
                            normal: {
                                show: false,
                                position: 'right',
                                //offset:[5, 0],
                                color: '#0f0',
                                formatter: '{b}',
                                textStyle: {
                                    color: "#0f0"
                                }
                            },
                            emphasis: {
                                show: false,
                                color: "#f60"
                            }
                        },
                        symbol: 'pin',
                        symbolSize: 50,
                        data: [mapDataList[0]]
                    }
                ]
            }

            // 绘制图表
            chartZGDT3.setOption(optionZGDT3, true);
            
        }
    }
}
</script>

<style lang='less'>
</style>

如有漏洞,欢迎互动指教!!!!!!!!!!!!!

在地图上画出各省边界,北京市密云县json内容如下: {"type": "FeatureCollection", "features": [{"type": "Feature","properties":{"id":"110228","name":"密云县","cp":[117.0923,40.5121],"childNum":1},"geometry":{"type":"Polygon","coordinates":[[[116.7586,40.7064],[116.7847,40.7016],[116.7888,40.714],[116.7902,40.727],[116.7819,40.7524],[116.7833,40.7579],[116.7902,40.7517],[116.8025,40.7462],[116.819,40.7504],[116.8272,40.749],[116.8396,40.7607],[116.8341,40.7703],[116.8506,40.7751],[116.8506,40.7785],[116.8671,40.7847],[116.8629,40.7922],[116.8712,40.7943],[116.8739,40.7991],[116.8808,40.7977],[116.8863,40.8012],[116.8959,40.7936],[116.8945,40.7819],[116.8973,40.7778],[116.9234,40.7737],[116.9247,40.7627],[116.9289,40.7565],[116.9234,40.7524],[116.9261,40.7449],[116.9412,40.7401],[116.9453,40.7284],[116.9563,40.7229],[116.9659,40.714],[116.9646,40.7092],[117.0044,40.6968],[117.0195,40.6954],[117.0305,40.692],[117.0497,40.7002],[117.084,40.7023],[117.0964,40.7057],[117.1115,40.7071],[117.117,40.6995],[117.1225,40.7009],[117.1527,40.6968],[117.1664,40.6989],[117.1788,40.6934],[117.1829,40.6975],[117.2063,40.6947],[117.231,40.6844],[117.242,40.6769],[117.2612,40.681],[117.2777,40.6666],[117.2914,40.6597],[117.3257,40.6604],[117.3367,40.6631],[117.3367,40.6666],[117.3463,40.6741],[117.3615,40.6748],[117.3903,40.6837],[117.4068,40.6865],[117.4205,40.6865],[117.4507,40.6796],[117.4644,40.6734],[117.4796,40.6776],[117.4947,40.6748],[117.5056,40.6666],[117.507,40.6549],[117.5029,40.6528],[117.5015,40.6364],[117.4796,40.6357],[117.4713,40.6474],[117.448,40.6419],[117.4301,40.6405],[117.4219,40.6377],[117.4178,40.6185],[117.4136,40.6062],[117.4205,40.5945],[117.4232,40.5821],[117.4301,40.5787],[117.426,40.5732],[117.4205,40.5691],[117.404,40.5739],[117.4013,40.5698],[117.3944,40.567],[117.3889,40.5615],[117.3766,40.5663],[117.3656,40.5759],[117.3505,40.5801],[117.334,40.5766],[117.3106,40.5766],[117.2955,40.567],[117.2667,40.5581],[117.2502,40.5485],[117.253,40.5416],[117.2502,40.5375],[117.2598,40.5196],[117.2475,40.5121],[117.2392,40.5176],[117.2337,40.5135],[117.2131,40.5128],[117.2104,40.5073],[117.209,40.497],[117.2173,40.4949],[117.2282,40.4812],[117.2255,40.4778],[117.2365,40.4688],[117.2351,40.4578],[117.2639,40.4414],[117.2571,40.4297],[117.2502,40.429],[117.2337,40.4166],[117.2324,40.4036],[117.2392,40.3988],[117.2406,40.3947],[117.2365,40.394],[117.2351,40.3892],[117.2282,40.3857],[117.2241,40.3754],[117.2186,40.3775],[117.209,40.3734],[117.1925,40.3761],[117.1774,40.3748],[117.1637,40.3706],[117.1472,40.372],[117.1472,40.3672],[117.139,40.3624],[117.1184,40.3548],[117.1129,40.3542],[117.1005,40.3603],[117.0593,40.3377],[117.0538,40.337],[117.0525,40.3439],[117.0374,40.3473],[117.0236,40.337],[117.0126,40.326],[117.0113,40.3082],[117.0058,40.3027],[117.0003,40.302],[117.0044,40.2958],[116.9728,40.2848],[116.9522,40.2621],[116.9495,40.2567],[116.9659,40.2374],[116.9536,40.2292],[116.9412,40.2237],[116.9316,40.2306],[116.9206,40.223],[116.9028,40.2244],[116.8973,40.2319],[116.8918,40.2347],[116.8932,40.2415],[116.8739,40.2683],[116.8753,40.2759],[116.8712,40.2889],[116.8588,40.2917],[116.8492,40.3102],[116.8341,40.3082],[116.8272,40.2985],[116.8231,40.2862],[116.819,40.2834],[116.808,40.2869],[116.7888,40.2889],[116.7833,40.2793],[116.7696,40.2731],[116.7517,40.2752],[116.7448,40.2793],[116.7352,40.2793],[116.7435,40.2889],[116.7435,40.2944],[116.7668,40.3143],[116.7696,40.3219],[116.7682,40.3253],[116.7517,40.3267],[116.7503,40.3315],[116.7448,40.3349],[116.7448,40.3391],[116.7297,40.3349],[116.7311,40.3391],[116.7229,40.3397],[116.727,40.361],[116.7188,40.361],[116.7188,40.3693],[116.7133,40.3686],[116.7064,40.3761],[116.7119,40.3802],[116.7091,40.3871],[116.7119,40.3919],[116.7215,40.396],[116.7242,40.4036],[116.7242,40.4166],[116.7201,40.4235],[116.7188,40.4297],[116.7201,40.4386],[116.7229,40.4427],[116.7229,40.4475],[116.7078,40.4661],[116.7009,40.4812],[116.6927,40.4881],[116.6982,40.4942],[116.7009,40.508],[116.7119,40.5183],[116.7133,40.5231],[116.7174,40.5251],[116.7091,40.532],[116.7091,40.543],[116.6776,40.5519],[116.6844,40.554],[116.6982,40.5656],[116.7064,40.5643],[116.7146,40.5746],[116.7119,40.5849],[116.7091,40.5904],[116.7105,40.6007],[116.705,40.6027],[116.7078,40.6075],[116.7023,40.6151],[116.7064,40.624],[116.7023,40.6316],[116.7119,40.6425],[116.7119,40.6556],[116.7091,40.6618],[116.7146,40.668],[116.7146,40.6796],[116.7311,40.6913],[116.7476,40.6968],[116.7586,40.7064]]]}} ]}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值