当前位置:   article > 正文

echarts实现地图_echarts显示地图

echarts显示地图

文章以河南省为例

一、先下载eacherts相关js文件(echarts.min.js)和echarts使用到的地图插件(map)

ecarts.min.js在echarts官网下载,map插件下载地址:GitHub - zhxiangfei/echarts-map: echarts-map地图(全国+各省) (包含全国、各省城市)

二、代码

  1. <script type="text/javascript" src="/home/js/jquery.js"></script>
  2. <script type="text/javascript" src="/home/js/echarts.min.js"></script>
  3. <-- 我这里用的是河南地图,可以根据自己需要选择城市-->
  4. <script type="text/javascript" src="/home/js/map/js/province/henan.js"></script>
  5. <script>
  6. <--地址换成从github下载的文件路径-->
  7. var uploadedDataURL = "/home/js/map/json/province/henan.json";
  8. myChart.showLoading();
  9. $.getJSON(uploadedDataURL, function(geoJson) {
  10. echarts.registerMap('henan', geoJson);
  11. myChart.hideLoading();
  12. var geoCoordMap = {
  13. '兰考': [114.385481, 34.825227],
  14. '郑州市': [113.4668, 34.6234],
  15. '开封市': [114.311523, 34.809969],
  16. '洛阳市': [112.471793, 34.62387],
  17. '平顶山市': [113.198486, 33.775454],
  18. '安阳市': [114.39891, 36.108225],
  19. '鹤壁市': [114.306924, 35.752846],
  20. '新乡市': [113.938978, 35.309189],
  21. '焦作市': [113.249079, 35.222425],
  22. '濮阳市': [115.038216, 35.764093],
  23. '许昌市': [113.860789, 34.040073],
  24. '漯河市': [114.021765, 33.590883],
  25. '三门峡市': [111.179383, 34.783409],
  26. '南阳市': [112.531584, 33.019279],
  27. '商丘市': [115.659125, 34.44116],
  28. '信阳市': [114.095355, 32.146995],
  29. '周口市': [114.693267, 33.644758],
  30. '驻马店市': [114.030964, 33.034777],
  31. '济源市': [112.623571, 35.078872],
  32. };
  33. var moveLine = {
  34. 'normal': [
  35. {"fromName": "兰考","toName": "郑州市",'coords':[[114.385481, 34.825227],[113.4668, 34.6234]]},
  36. {"fromName": "兰考","toName": "开封市",'coords': [[114.385481, 34.825227],[114.311523, 34.809969]]},
  37. {"fromName": "兰考","toName": "洛阳市",'coords': [[114.385481, 34.825227],[112.471793, 34.62387]]},
  38. {"fromName": "兰考","toName": "平顶山市",'coords': [[114.385481, 34.825227],[113.198486, 33.775454]]},
  39. {"fromName": "兰考","toName": "安阳市",'coords': [[114.385481, 34.825227],[114.39891, 36.108225]]},
  40. {"fromName": "兰考","toName": "鹤壁市",'coords': [[114.385481, 34.825227],[114.306924, 35.752846]]},
  41. {"fromName": "兰考","toName": "新乡市",'coords': [[114.385481, 34.825227],[113.938978, 35.309189]]},
  42. {"fromName": "兰考","toName": "焦作市",'coords': [[114.385481, 34.825227],[113.249079, 35.222425]]},
  43. {"fromName": "兰考","toName": "濮阳市",'coords': [[114.385481, 34.825227],[115.038216, 35.764093]]},
  44. {"fromName": "兰考","toName": "许昌市",'coords': [[114.385481, 34.825227],[113.860789, 34.040073]]},
  45. {"fromName": "兰考","toName": "漯河市",'coords': [[114.385481, 34.825227],[114.021765, 33.590883]]},
  46. {"fromName": "兰考","toName": "三门峡市",'coords': [[114.385481, 34.825227],[111.179383, 34.783409]]},
  47. {"fromName": "兰考","toName": "南阳市",'coords': [[114.385481, 34.825227],[112.531584, 33.019279]]},
  48. {"fromName": "兰考","toName": "商丘市",'coords': [[114.385481, 34.825227],[115.659125, 34.44116]]},
  49. {"fromName": "兰考","toName": "信阳市",'coords': [[114.385481, 34.825227],[114.095355, 32.146995]]},
  50. {"fromName": "兰考","toName": "周口市",'coords': [[114.385481, 34.825227],[114.693267, 33.644758]]},
  51. {"fromName": "兰考","toName": "驻马店市",'coords': [[114.385481, 34.825227],[114.030964, 33.034777]]},
  52. {"fromName": "兰考","toName": "济源市",'coords': [[114.385481, 34.825227],[112.623571, 35.078872]]},
  53. ],
  54. 'warning': [
  55. ]
  56. };
  57. var data = [{
  58. name: '郑州市',
  59. value: 190
  60. },
  61. {
  62. name: '开封市',
  63. value: 190
  64. },
  65. {
  66. name: '洛阳市',
  67. value: 190
  68. },
  69. {
  70. name: '平顶山市',
  71. value: 190
  72. },
  73. {
  74. name: '安阳市',
  75. value: 190
  76. },
  77. {
  78. name: '鹤壁市',
  79. value: 90
  80. },
  81. {
  82. name: '新乡市',
  83. value: 120
  84. },
  85. {
  86. name: '焦作市',
  87. value: 120
  88. },
  89. {
  90. name: '濮阳市',
  91. value: 120
  92. },
  93. {
  94. name: '许昌市',
  95. value: 120
  96. },
  97. {
  98. name: '漯河市',
  99. value: 190
  100. },
  101. {
  102. name: '三门峡市',
  103. value: 190
  104. },
  105. {
  106. name: '南阳市',
  107. value: 190
  108. },
  109. {
  110. name: '商丘市',
  111. value: 190
  112. },
  113. {
  114. name: '信阳市',
  115. value: 190
  116. },
  117. {
  118. name: '周口市',
  119. value: 190
  120. },
  121. {
  122. name: '驻马店市',
  123. value: 90
  124. },
  125. {
  126. name: '济源市',
  127. value: 90
  128. },
  129. ];
  130. var max = 480,
  131. min = 9; // todo
  132. var maxSize4Pin = 100,
  133. minSize4Pin = 20;
  134. var convertData = function(data) {
  135. var res = [];
  136. for (var i = 0; i < data.length; i++) {
  137. var geoCoord = geoCoordMap[data[i].name];
  138. if (geoCoord) {
  139. res.push({
  140. name: data[i].name,
  141. value: geoCoord.concat(data[i].value)
  142. });
  143. }
  144. }
  145. return res;
  146. };
  147. option = {
  148. backgroundColor: '#091c3d',
  149. // title: {
  150. // top: 20,
  151. // text: '',
  152. // subtext: '',
  153. // x: 'center',
  154. // textStyle: {
  155. // color: '#ccc'
  156. // }
  157. // },
  158. title: {
  159. text: '河南省中医院分布图',
  160. x: 'center',
  161. textStyle: {
  162. color: '#fff'
  163. }
  164. },
  165. tooltip: {
  166. trigger: 'item',
  167. formatter: function(params) {
  168. console.log(params.value)
  169. // if (typeof(params.value)[2] == "undefined") {
  170. // return params.name + ' : ' + params.value;
  171. // } else {
  172. return params.name + ' : ' + params.value[2];
  173. // }
  174. }
  175. },
  176. legend: {
  177. orient: 'vertical',
  178. y: 'bottom',
  179. x: 'right',
  180. data: ['pm2.5'],
  181. textStyle: {
  182. color: '#fff'
  183. }
  184. },
  185. visualMap: {
  186. show: false,
  187. min: 0,
  188. max: 500,
  189. left: 'left',
  190. top: 'bottom',
  191. text: ['高', '低'], // 文本,默认为数值文本
  192. calculable: true,
  193. seriesIndex: [1],
  194. inRange: {
  195. // color: ['#9074c3', '#4487d5', '#7e9e8e', '#51a2c2']
  196. // color: ['#3B5077', '#031525'] // 蓝黑
  197. // color: ['#ffc0cb', '#800080'] // 红紫
  198. // color: ['#3C3B3F', '#605C3C'] // 黑绿
  199. //color: ['#0f0c29', '#302b63', '#24243e'] // 黑紫黑
  200. //color: ['#23074d', '#cc5333'] // 紫红
  201. // color: ['#00467F', '#A5CC82'] // 蓝绿
  202. // color: ['#1488CC', '#2B32B2'] // 浅蓝
  203. // color: ['#00467F', '#A5CC82'] // 蓝绿
  204. }
  205. },
  206. geo: {
  207. show: true,
  208. map: 'henan',
  209. label: {
  210. normal: {
  211. show: false
  212. },
  213. emphasis: {
  214. show: false,
  215. }
  216. },
  217. roam: true,
  218. itemStyle: {
  219. normal: {
  220. areaColor: 'transparent',
  221. borderColor: '#3fdaff',
  222. borderWidth: 2,
  223. shadowColor: 'rgba(63, 218, 255, 0.5)',
  224. shadowBlur: 30
  225. },
  226. emphasis: {
  227. areaColor: '#2B91B7',
  228. }
  229. }
  230. },
  231. series: [
  232. {
  233. type: 'map',
  234. map: 'henan',
  235. geoIndex: 0,
  236. aspectScale: 0.75, //长宽比
  237. showLegendSymbol: false, // 存在legend时显示
  238. label: {
  239. normal: {
  240. show: true
  241. },
  242. emphasis: {
  243. show: false,
  244. textStyle: {
  245. color: '#fff'
  246. }
  247. }
  248. },
  249. roam: true,
  250. itemStyle: {
  251. normal: {
  252. areaColor: '#031525',
  253. borderColor: '#FFFFFF',
  254. },
  255. emphasis: {
  256. areaColor: '#2B91B7'
  257. }
  258. },
  259. animation: false,
  260. data: data
  261. },
  262. {
  263. name: '城市',
  264. type: 'effectScatter',
  265. coordinateSystem: 'geo',
  266. zlevel: 1,
  267. showEffectOn: 'render',
  268. rippleEffect: {
  269. scale: 3,
  270. brushType: 'stroke'
  271. },
  272. hoverAnimation: true, //鼠标移上去显示数据
  273. label: {
  274. normal: {
  275. show: true,
  276. position: 'right',
  277. formatter: '{b}'
  278. },
  279. emphasis: {
  280. show: true
  281. }
  282. },
  283. symbolSize: function (val) {
  284. return val[2] / 10;
  285. },
  286. itemStyle: {
  287. normal: {
  288. color: '#F4E925',
  289. shadowBlur: 10,
  290. shadowColor: '#05C3F9'
  291. }
  292. },
  293. data: convertData(data),
  294. },
  295. // 射线样式
  296. {
  297. name: '线路',
  298. type: 'lines',
  299. coordinateSystem: 'geo',
  300. zlevel: 2, //数值越大越在上面
  301. large: true,
  302. symbol: ['none', 'arrow'],
  303. effect: {
  304. show: true,
  305. period: 6,
  306. constantSpeed: 30,
  307. trailLength: 0,
  308. symbolSize:3 //圆点大小
  309. },
  310. lineStyle: {
  311. normal: {
  312. // 线条颜色
  313. color:'#0fff17',
  314. width: 0.5,
  315. opacity: 0.6,
  316. curveness: 0.2
  317. }
  318. },
  319. data: moveLine.normal
  320. },
  321. // 射线样式
  322. {
  323. name: '线路',
  324. type: 'lines',
  325. coordinateSystem: 'geo',
  326. zlevel: 3,
  327. large: true,
  328. effect: {
  329. show: true,
  330. period: 5,
  331. trailLength: 0.8, //特效尾迹的长度
  332. color: '#fff',
  333. symbolSize: 3 //圆点大小
  334. },
  335. lineStyle: {
  336. normal: {
  337. // color:'#0fff17',
  338. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
  339. offset: 0,
  340. color: '#0fff17'
  341. }, {
  342. offset: 1,
  343. color: '#F4E925'
  344. }], false),
  345. width: 0.2,
  346. curveness: 0.2
  347. }
  348. },
  349. data: moveLine.normal
  350. }
  351. ]
  352. };
  353. myChart.setOption(option);
  354. });
  355. </script>

三、效果图

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/108711?site
推荐阅读