Echarts 部分地图展示

时间 2021/7/15 16:35:02 加载中...

功能

最近要实现如下这样的功能

效果查看:
http://sqber.com/file/map/jingjidemo.html

部分省份地图

中国地图

实现思路

实现思路如下:

思路一

通过 Echarts 的地图功能来实现,在 Geo 属性 中添加两个地图,一个是世界地图,另一个就是我们要突出显示的地图。

当前我们要获取到对应的 GeoJSON 数据。比如第一个只显示部分省份的,就可以只用部分省份的 GeoJSON 数据。

GeoJSON 获取
https://github.com/echarts-maps
http://datav.aliyun.com/tools/atlas/index.html#&lat=31.769817845138945&lng=104.29901249999999&zoom=4

然后调整两个地图的 中心点 和 缩放 级别,另两个地图重合显示。
设置 Roam 属性为 true,可以进行调整,调整好之后,通过控制台调用 getInfo 方法记录下中心点和缩放级别的信息。

上面的黄点信息是设置 series 来实现的,series 中的地图要使用高亮的地图,保证点的位置的准确性。

思路二

Geo 中引入一个地图,然后在 Series 中引入高亮的地图。

思路三

Geo 中只引入一个地图,通过 Region 属性高亮显示的区域。
这个比较麻烦,可能得把所有的高亮 Region 都得列举出来。且你要分别控制不同区域的鼠标移动上去的效果。

具体代码

思路一的具体实现:(另外两个不考虑了)

效果查看:
http://sqber.com/file/map/jingjidemo.html

具体代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>北京地图</title>
  7. <style>
  8. * {
  9. margin: 0;
  10. padding: 0;
  11. }
  12. #container {
  13. margin: 0 auto;
  14. position: absolute;
  15. top:0;
  16. right:0;
  17. bottom: 0;
  18. left:0;
  19. }
  20. </style>
  21. </head>
  22. <body style="">
  23. <div id="container"></div>
  24. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
  25. <script src="https://cdn.bootcdn.net/ajax/libs/echarts/4.8.0/echarts.min.js"></script>
  26. <script src="worldGeo.json"></script>
  27. <script src="lnglat.json"></script>
  28. <script>
  29. var myChart = echarts.init(document.getElementById('container'));
  30. function getGeoJson() {
  31. // var arr = [
  32. // 'https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=510000', //四川
  33. // 'https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=530000', //云南
  34. // 'https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=520000', //贵州
  35. // 'https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=500000', //重庆
  36. // 'https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=420000', //湖北
  37. // 'https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=430000', //湖南
  38. // 'https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=340000', // 安徽
  39. // 'https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=360000', // 江西
  40. // 'https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=310000', // 江西
  41. // 'https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=320000', // 江西
  42. // 'https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=330000', // 江西
  43. // //'http://sqber.com/file/echarts/wordGeo',
  44. // ]
  45. var mapJson = { "type": "FeatureCollection", "features": [] }
  46. worldGeo.features.map(fitem => mapJson.features.push(fitem))
  47. // arr.map(item => {
  48. // $.ajax({
  49. // type: "GET",
  50. // url: item,
  51. // async: false,
  52. // success: function (data) {
  53. // data.features.map(fitem => mapJson.features.push(fitem))
  54. // },
  55. // error: function (msg) {
  56. // console.error(msg);
  57. // }
  58. // })
  59. // })
  60. return mapJson
  61. }
  62. var mapJson = getGeoJson()
  63. echarts.registerMap('world', mapJson);
  64. function getGeoJson2() {
  65. var arr = [
  66. // 如果报错的话,把下面的 JSON 文件放在本地
  67. 'https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=510000', //四川
  68. 'https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=530000', //云南
  69. 'https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=520000', //贵州
  70. 'https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=500000', //重庆
  71. 'https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=420000', //湖北
  72. 'https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=430000', //湖南
  73. 'https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=340000', // 安徽
  74. 'https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=360000', // 江西
  75. 'https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=310000', // 江西
  76. 'https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=320000', // 江西
  77. 'https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=330000', // 江西
  78. ]
  79. var mapJson = { "type": "FeatureCollection", "features": [] }
  80. //worldGeo.features.map(fitem => mapJson.features.push(fitem))
  81. arr.map(item => {
  82. $.ajax({
  83. type: "GET",
  84. url: item,
  85. async: false,
  86. success: function (data) {
  87. data.features.map(fitem => mapJson.features.push(fitem))
  88. },
  89. error: function (msg) {
  90. console.error(msg);
  91. }
  92. })
  93. })
  94. return mapJson
  95. }
  96. var mapJson2 = getGeoJson2()
  97. echarts.registerMap('hunhe2', mapJson2);
  98. // 渐变色
  99. var areaColor = {
  100. type: 'radial',
  101. x: 0.5,
  102. y: 0.5,
  103. r: 0.5,
  104. colorStops: [{
  105. offset: 0, color: '#2381F3' // 0% 处的颜色
  106. }, {
  107. offset: 1, color: '#2C54CE' // 100% 处的颜色
  108. }],
  109. global: false // 缺省为 false
  110. }
  111. var option = {
  112. tooltip: {
  113. trigger: 'item',
  114. formatter:function(params, ticket, callback){
  115. if(params.data){
  116. return params.name + " : " + params.data.value[2]
  117. }
  118. else{
  119. return params.name
  120. }
  121. }
  122. },
  123. title: {
  124. text: '',
  125. x: "center",
  126. },
  127. backgroundColor: {
  128. type: 'linear',
  129. x: 0,
  130. y: 0,
  131. x2: 1,
  132. y2: 1,
  133. colorStops: [
  134. {
  135. offset: 0,
  136. color: '#0f378f', // 0% 处的颜色
  137. },
  138. {
  139. offset: 1,
  140. color: '#00091a', // 100% 处的颜色
  141. },
  142. ],
  143. globalCoord: false, // 缺省为 false
  144. },
  145. visualMap: {
  146. show: false,
  147. min: 0,
  148. max: 500,
  149. left: 'left',
  150. top: 'bottom',
  151. text: ['高', '低'], // 文本,默认为数值文本
  152. calculable: true,
  153. seriesIndex: [1],
  154. inRange: {},
  155. },
  156. geo: [
  157. {
  158. map: 'world',
  159. label: {
  160. show:false
  161. },
  162. silent:true,
  163. roam: true,
  164. center: [110.39276699313069,28.723695629153493],
  165. zoom: 12.9687122980557,
  166. itemStyle: {
  167. normal: {
  168. areaColor: '#06235F',
  169. borderColor: '#0C4BAA',
  170. borderWidth: 1,
  171. },
  172. emphasis: {
  173. areaColor: '#FFAE00',
  174. }
  175. }
  176. },
  177. {
  178. map: 'hunhe2',
  179. label: {
  180. show:true,
  181. color:'white',
  182. fontSize:20
  183. },
  184. silent:true,
  185. roam: true,
  186. center: [110.44997122677593, 28.753268447939853],
  187. zoom: 0.9090909090909095,
  188. itemStyle: {
  189. normal: {
  190. areaColor: areaColor,
  191. borderColor: '#0C4BAA',
  192. borderWidth: 1,
  193. },
  194. emphasis: {
  195. areaColor: '#FFAE00',
  196. }
  197. }
  198. },
  199. ],
  200. series: [
  201. // {
  202. // name: '这是个地图',
  203. // type: 'map',
  204. // mapType: 'hunhe2',
  205. // label: {
  206. // normal: {
  207. // textStyle: {
  208. // fontSize: 15,
  209. // fontWeight: 'bold',
  210. // color: '#fff',
  211. // },
  212. // },
  213. // },
  214. // //aspectScale: 1,
  215. // roam: true,
  216. // center: [107.51392060163428,35.18339937096202],
  217. // zoom: 1.610510000000001,
  218. // itemStyle: {
  219. // normal: {
  220. // label: { show: false }, // 不显示地图上得省份名字
  221. // color: 'yellow',
  222. // borderWidth: 0.5, //区域边框宽度
  223. // borderColor: '#36D2FF', //区域边框颜色
  224. // areaColor: areaColor, //区域颜色
  225. // },
  226. // emphasis: {
  227. // label: { show: true },
  228. // borderWidth: 0.5,
  229. // borderColor: '#4b0082',
  230. // areaColor: '#ffdead',
  231. // },
  232. // },
  233. // },
  234. {
  235. type: 'effectScatter',
  236. coordinateSystem: 'geo',
  237. geoIndex: 1, // 这里指定使用哪个地图
  238. symbolSize: function (val, params) {
  239. var zoom = 10
  240. let result = val[2] / zoom
  241. if (parseFloat(val[2]) === 0) {
  242. result = 0.0000001 / zoom
  243. }
  244. result = Math.max(result, 5) // 最小是5
  245. result = Math.min(55, result) // 最大时35
  246. return result
  247. },
  248. data:[
  249. {
  250. name: '四川省',
  251. value: [120.185, 30.274, 29999]
  252. }
  253. ],
  254. showEffectOn: 'render',
  255. rippleEffect: {
  256. brushType: 'stroke'
  257. },
  258. hoverAnimation: true,
  259. label: {
  260. show: false
  261. },
  262. // label: {
  263. // show: false,
  264. // normal: {
  265. // // "formatter": "{c}",
  266. // formatter: function (item) {
  267. // // console.log(item);
  268. // // alert(item);
  269. // // return item;
  270. // return item.value[2]
  271. // },
  272. // position: 'right',
  273. // show: true
  274. // },
  275. // emphasis:{
  276. // label:true,
  277. // formatter: function (item) {
  278. // // console.log(item);
  279. // // alert(item);
  280. // // return item;
  281. // return item.value[2]
  282. // },
  283. // }
  284. // },
  285. itemStyle: {
  286. show : false,
  287. normal: {
  288. color: '#f4e925',
  289. shadowBlur: 10,
  290. shadowColor: '#333'
  291. }
  292. },
  293. zlevel: 1
  294. }]
  295. }
  296. myChart.setOption(option);
  297. function getInfo(){
  298. var centerInfo = myChart.getOption().geo[0].center
  299. var zoomInfo = myChart.getOption().geo[0].zoom
  300. var dataCenterInfo = myChart.getOption().geo[1].center
  301. var dataZoomInfo = myChart.getOption().geo[1].zoom
  302. console.log('backMap:' + centerInfo + " " + zoomInfo)
  303. console.log('frontMap:' + dataCenterInfo + " " + dataZoomInfo)
  304. }
  305. </script>
  306. </body>
  307. </html>

扫码分享
版权说明
作者:SQBER
文章来源:http://www.sqber.com/articles/echarts-part-map-show.html
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。