1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
| <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>ECharts 5.x 省市区地图切换</title> <script src="https://cdn.jsdelivr.net/npm/echarts@5.5.1/dist/echarts.min.js"></script> <style> #map-container { width: 100%; height: 600px; margin: 20px 0; } #info-panel { text-align: center; padding: 20px; background: #f5f5f5; border-radius: 8px; margin: 0 20px; } #back-btn { display: none; padding: 10px 20px; background: #1890ff; color: white; border: none; border-radius: 4px; cursor: pointer; margin-bottom: 10px; } </style> </head> <body> <div style="max-width: 1200px; margin: 0 auto; padding: 20px;"> <button id="back-btn">返回上一级</button> <div id="map-container"></div> <div id="info-panel"> <h2 id="area-name">中国</h2> <p id="area-content">点击地图上的省份,进入该省份地图查看详情</p> </div> </div>
<script> const chartDom = document.getElementById('map-container'); const myChart = echarts.init(chartDom);
let currentLevel = 'country'; let currentArea = 'china'; let mapDataCache = {};
const mapPathMap = { 'china': './map-data/china.json', '湖北省': './map-data/hubei.json', '武汉市': './map-data/wuhan.json' };
async function loadAndRegisterMap(areaName) { if (mapDataCache[areaName]) { return mapDataCache[areaName]; } const mapPath = mapPathMap[areaName]; if (!mapPath) { alert(`暂无 ${areaName} 的地图数据`); return null; } try { const response = await fetch(mapPath); const geoJSON = await response.json(); echarts.registerMap(areaName, geoJSON); mapDataCache[areaName] = geoJSON; return geoJSON; } catch (error) { console.error('地图数据加载失败:', error); alert('地图数据加载失败,请检查路径'); return null; } }
function getMapOption(areaName, mapType) { return { title: { text: areaName, left: 'center', top: 10 }, tooltip: { trigger: 'item', formatter: '{b}' }, visualMap: { min: 0, max: 100, left: 'left', top: 'bottom', text: ['高', '低'], calculable: true, inRange: { color: ['#e0f3f8', '#abd9e9', '#74add1', '#4575b4', '#313695'] } }, series: [ { name: areaName, type: 'map', map: mapType, roam: true, selectedMode: 'single', label: { show: true, fontSize: 12 }, emphasis: { label: { show: true, fontSize: 14 }, itemStyle: { areaColor: '#ffd700' } }, data: [ { name: '湖北省', value: 80 }, { name: '武汉市', value: 90 } ] } ] }; }
function updateInfoPanel(areaName) { document.getElementById('area-name').innerText = areaName; document.getElementById('area-content').innerText = `${areaName} 的详细内容区域,可在这里展示业务数据、图表等`; }
async function switchMap(areaName) { const geoJSON = await loadAndRegisterMap(areaName); if (!geoJSON) return;
const option = getMapOption(areaName, areaName); myChart.setOption(option, true);
updateInfoPanel(areaName);
const backBtn = document.getElementById('back-btn'); backBtn.style.display = currentLevel === 'country' ? 'none' : 'block'; }
myChart.on('click', async function (params) { const clickedArea = params.name; if (!clickedArea) return;
if (currentLevel === 'country') { currentLevel = 'province'; currentArea = clickedArea; await switchMap(clickedArea); } else if (currentLevel === 'province') { currentLevel = 'city'; currentArea = clickedArea; await switchMap(clickedArea); } });
document.getElementById('back-btn').addEventListener('click', async function () { if (currentLevel === 'city') { currentLevel = 'province'; currentLevel = 'country'; currentArea = 'china'; await switchMap('china'); } else if (currentLevel === 'province') { currentLevel = 'country'; currentArea = 'china'; await switchMap('china'); } });
switchMap('china');
window.addEventListener('resize', function () { myChart.resize(); }); </script> </body> </html>
|