echarts绘制上海市乡镇级地图

echarts绘制上海市乡镇级地图

echarts 最新版本不支持GeometryCollection,虽然有人在4年前就提了issue
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="./jquery-latest.js"></script>
    <script src="./echarts.js"></script>
    <title>上海市镇级地图</title>
</head>
<body>
    <div id="map" style="width: 600px;height: 700px;margin: 0 auto"></div>
</body>
<script>
    var myChart = echarts.init(document.getElementById('map'));
    $.get("./sh.json",function(geoJson){
        echarts.registerMap('sh',geoJson,{});
        var option = {
            series: [
                {
                    type: 'map',
                    mapType: 'sh',
                    aspectScale: 0.85
                }                              
            ]
        };
        myChart.setOption(option);
    });
</script>
</html>
需改造echarts.js中的parseGeoJSON函数
function parseGeoJSON(geoJson, nameProperty) {
		
      geoJson = decode(geoJson);
      return map(filter(geoJson.features, function (featureObj) {
        // Output of mapshaper may have geometry null
		
		console.info(featureObj)
        return featureObj.geometry && featureObj.properties 
		&& (
                // avoid length error if missing coordinates
                (featureObj.geometry.coordinates && featureObj.geometry.coordinates.length > 0)
                // allow GeometryCollection
                || (featureObj.geometry.geometries && featureObj.geometry.geometries.length > 0)
            )
        
		;
      }), function (featureObj) {
        var properties = featureObj.properties;
        var geo = featureObj.geometry;
        var geometries = [];

        switch (geo.type) {
          case 'Polygon':
            var coordinates = geo.coordinates; // According to the GeoJSON specification.
            // First must be exterior, and the rest are all interior(holes).

            geometries.push(new GeoJSONPolygonGeometry(coordinates[0], coordinates.slice(1)));
            break;

          case 'MultiPolygon':
            each(geo.coordinates, function (item) {
              if (item[0]) {
                geometries.push(new GeoJSONPolygonGeometry(item[0], item.slice(1)));
              }
            });
            break;

          case 'LineString':
            geometries.push(new GeoJSONLineStringGeometry([geo.coordinates]));
            break;
            // 支持GeometryCollection
		case 'GeometryCollection':
            var geometries2 = geo.geometries;
            each$1(geometries2, function (geo) { // OR      zrUtil.each(geometries2, function (geo) {
                var coordinates = geo.coordinates;
                if (geo.type === 'Polygon') { // this is a full copy from above
                    geometries.push({
                        type: 'polygon',
                        exterior: coordinates[0],
                        interiors: coordinates.slice(1)
                    });
                } // end full copy
            });
            break;
			
          case 'MultiLineString':
            geometries.push(new GeoJSONLineStringGeometry(geo.coordinates));
        }

        var region = new GeoJSONRegion(properties[nameProperty || 'name'], geometries, properties.cp);
        region.properties = properties;
        return region;
      });
    }

成效
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用ECharts绘制武安市的地图并标记坐标点非常简单。以下是一个简单的步骤指南: 1. 首先,确保你已经引入了ECharts库的JavaScript文件。 2. 创建一个div元素,用于容纳地图。 ```html <div id="map" style="width: 800px; height: 600px;"></div> ``` 3. 在JavaScript文件中,使用echarts.init方法初始化地图,并设置图表的宽度和高度。 ```javascript var myChart = echarts.init(document.getElementById('map')); ``` 4. 定义地图的配置项,包括地图类型和数据。 ```javascript var option = { tooltip: { trigger: 'item', formatter: '{b}' }, series: [ { name: '武安市', type: 'map', mapType: '武安市', roam: false, label: { show: true, position: 'inside' }, data: [] // 在这里填入你的数据 } ] }; ``` 5. 使用echarts.registerMap方法注册地图。 ```javascript echarts.registerMap('武安市', 武安市的地图数据); ``` 6. 将配置项应用到图表中。 ```javascript myChart.setOption(option); ``` 7. 若要标记坐标点,可以在数据项中添加坐标点的地理位置信息。 ```javascript data: [ {name: '坐标点1', value: [经度, 纬度]}, {name: '坐标点2', value: [经度, 纬度]}, // ... ] ``` 8. 最后要根据数据项中的坐标点进行标记。 ```javascript series: [ { name: '武安市', type: 'map', mapType: '武安市', roam: false, label: { show: true, position: 'inside' }, data: [ {name: '坐标点1', value: [经度, 纬度]}, {name: '坐标点2', value: [经度, 纬度]}, // ... ], markPoint: { symbolSize: 10, label: { show: false }, itemStyle: { color: 'red' }, data: [ {name: '坐标点1', value: [经度, 纬度]}, {name: '坐标点2', value: [经度, 纬度]}, // ... ] } } ] ``` 以上就是使用ECharts绘制武安市的地图并标记坐标点的基本步骤。你可以根据实际需要修改配置项和数据。希望对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值