Echarts3实例 加载地图

前言

    Echarts加载GeoJson数据生成canvas地图,可以实现地图简单的展示,获取GeoJson数据的方法见:https://blog.csdn.net/idomyway/article/details/85072698
    Echarts生成地图的数据加载方式有两种:

  • 加载GeoJson的js文件
  • 加GeoJson的Json文件

加载GeoJson的js文件显示地图

    通过js文件加载GeoJson地图数据需要在html中进行导入,如果在比较大的地图数据中,会加重网络负担,不推荐使用

加载GeoJson的json文件显示地图

    我们在需要加载地图地方加载json数据,并进行注册使用,在进行展示的时候再异步加载数据。prefect

实现效果

在这里插入图片描述

实现代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        html,body{
            height: 100%;
        }
        *{
            margin: 0px;
            height: 0px;
        }
        #map{
            width: 800px;
            height: 600px;
            border: 1px solid red;
        }
    </style>
    <script src="js/echarts.min.js"></script>
    <script src="js/jquery-3.1.1.js"></script>
    <!--<script src="json/shandong.js"></script>-->
</head>
<body>
<div id="map"></div>
<script>
    // JS
    // var chart = echarts.init(document.getElementById('map'));
    //     var option = {
    //         title: {
    //             text: '山东地图',
    //
    //         },
    //         series: [{
    //             type: 'map',
    //             map: 'shandong',
    //             aspectScale:1,//保持原始比例
    //             roam: true,
    //             label: {
    //                 show:true,
    //                 normal: {
    //                     show: true,
    //                     color:"#000",
    //                 },
    //                 emphasis: {
    //                     show: true,
    //                     fontSize:16,
    //                     color:"#fff"
    //
    //                 }
    //             },
    //         }]
    //     };
    // chart.setOption(option);

    // JSON
    $.getJSON('./json/shandong.json', function (data) {
        echarts.registerMap('shandong', data);
        var chart = echarts.init(document.getElementById('map'));
        var option = {
            title: {
                text: '山东地图',

            },
            series: [{
                type: 'map',
                map: 'shandong',
                aspectScale:1,//保持原始比例
                roam: true,
                label: {
                    show:true,
                    normal: {
                        show: true,
                        color:"#000",
                    },
                    emphasis: {
                        show: true,
                        fontSize:16,
                        color:"#fff"

                    }
                },
            }]
        };
        chart.setOption(option);
    });
</script>

</body>
</html>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在echarts加载全球百度地图,您需要按照以下步骤操作: 1. 首先,在您的HTML页面中引入echarts和百度地图API的JavaScript文件。 ```html <script src="https://cdn.bootcdn.net/ajax/libs/echarts/5.1.2/echarts.min.js"></script> <script type="text/javascript" src="https://api.map.baidu.com/api?v=3.0&ak=您的AK"></script> ``` 其中,AK是您在百度地图开发者平台申请的AK密钥。 2. 然后,在echarts中定义一个百度地图实例,并设置其属性。 ```javascript var bmap = new BMap.Map("mapContainer"); // 创建百度地图实例 var mapStyle = { styleJson: [ { "featureType": "water", "elementType": "all", "stylers": { "color": "#044161" } }, { "featureType": "land", "elementType": "all", "stylers": { "color": "#004981" } }, { "featureType": "boundary", "elementType": "geometry", "stylers": { "color": "#064f85" } }, { "featureType": "railway", "elementType": "all", "stylers": { "visibility": "off" } }, { "featureType": "highway", "elementType": "geometry", "stylers": { "color": "#004981" } }, { "featureType": "highway", "elementType": "geometry.fill", "stylers": { "color": "#005b96", "lightness": 1 } }, { "featureType": "highway", "elementType": "labels", "stylers": { "visibility": "off" } }, { "featureType": "arterial", "elementType": "geometry", "stylers": { "color": "#004981" } }, { "featureType": "arterial", "elementType": "geometry.fill", "stylers": { "color": "#00508b" } }, { "featureType": "poi", "elementType": "all", "stylers": { "visibility": "off" } }, { "featureType": "green", "elementType": "all", "stylers": { "color": "#056197", "visibility": "off" } }, { "featureType": "subway", "elementType": "all", "stylers": { "visibility": "off" } }, { "featureType": "manmade", "elementType": "all", "stylers": { "visibility": "off" } }, { "featureType": "local", "elementType": "all", "stylers": { "visibility": "off" } }, { "featureType": "arterial", "elementType": "labels", "stylers": { "visibility": "off" } }, { "featureType": "boundary", "elementType": "geometry.fill", "stylers": { "color": "#029fd4" } }, { "featureType": "building", "elementType": "all", "stylers": { "color": "#1a5787" } }, { "featureType": "label", "elementType": "all", "stylers": { "visibility": "off" } } ] }; bmap.setMapStyle(mapStyle); // 设置地图样式 bmap.centerAndZoom(new BMap.Point(116.404, 39.915), 3); // 初始化地图,设置中心点和缩放级别 bmap.enableScrollWheelZoom(true); // 启用滚轮缩放 ``` 在这里,我们创建了一个名为bmap的百度地图实例,并设置了地图样式、中心点和缩放级别、滚轮缩放等属性。 3. 最后,在echarts中使用百度地图实例作为地图组件的option。 ```javascript option = { bmap: { center: [116.404, 39.915], zoom: 3, roam: true, mapStyle: { styleJson: [ { "featureType": "water", "elementType": "all", "stylers": { "color": "#044161" } }, // ... ] } }, series: [{ type: 'scatter', coordinateSystem: 'bmap', data: [ [116.41637, 39.92856], // ... ], symbolSize: function (val) { return val[2] * 5; }, itemStyle: { color: 'purple' }, emphasis: { label: { show: true, formatter: function (param) { return param.data[3]; }, position: 'top' } } }] }; ``` 在这里,我们将百度地图实例作为地图组件的option,并设置了散点图的相关属性。 通过以上步骤,您就可以在echarts加载全球百度地图了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值