0


echarts加载区域地图,并标注点

效果如下,加载了南海区域的地图,并标注几个气象站点;

1、下载区域地图的JSON:DataV.GeoAtlas地理小工具系列

新建nanhai.json,把下载的JSON数据放进来

说明:如果第二步不打勾,只显示省的名字,

如果打勾的话,会显示省下所有市的名字,看个人需求

如果要把多个省放在一起展示,则把多个JSON文件里的features数据合并即可

2、使用Echarts展示地图

  1. <!--地图-->
  2. <div ref="chartRef" class="chart"/>
  3. <script setup>
  4. import {ref, onMounted} from 'vue'
  5. import * as echarts from 'echarts'
  6. import nanhaiJson from '@/assets/map/nanhai.json'
  7. //地图json数据: https://datav.aliyun.com/portal/school/atlas/area_selector
  8. const chartRef = ref()
  9. const formRef = ref()
  10. let myChart = null;
  11. const stationData = ref([])
  12. //加载数据
  13. onMounted(() => {
  14. //加载南海地图
  15. echarts.registerMap('nanhai', nanhaiJson)
  16. loadData()
  17. })
  18. const loadData = () => {
  19. xxApi.xxPage().then((data) => {
  20. if (data && data.total > 0) {
  21. stationData.value = []
  22. //拼接地图上需要标注的点
  23. data.records.forEach((item) => {
  24. stationData.value.push(
  25. {
  26. name: item.name,
  27. value: [item.longitude, item.latitude]
  28. }
  29. )
  30. })
  31. }
  32. loadChart()
  33. })
  34. }
  35. //加载图表
  36. const loadChart = () => {
  37. // 如果实例已经存在,则先销毁再重新创建
  38. if (myChart != null && myChart.dispose) {
  39. myChart.dispose();
  40. }
  41. myChart = echarts.init(chartRef.value)
  42. myChart.showLoading({text: 'loading'})
  43. let option = {
  44. geo: {
  45. map: 'nanhai',
  46. zoom: 1.2,//缩放比例
  47. roam: true, // 是否允许缩放和平移
  48. itemStyle: {
  49. areaColor: 'lightskyblue',
  50. borderColor: '#404a59'
  51. },
  52. label: {
  53. show: true
  54. },
  55. },
  56. //气象站点列表
  57. series: [{
  58. type: 'scatter',
  59. coordinateSystem: 'geo',
  60. data: stationData.value,
  61. symbolSize: 10,
  62. label: {
  63. show: true,
  64. formatter: function (params) {
  65. return params.name; // 显示点的name
  66. },
  67. position: 'top', // 或其他位置
  68. color: '#FF4500' // 设置标签字体颜色为红色
  69. },
  70. itemStyle: {
  71. normal: {
  72. color: '#FFA500' // 设置为橘黄色
  73. }
  74. },
  75. }]
  76. }
  77. myChart.hideLoading()
  78. myChart.setOption(option)
  79. // 自适应屏幕
  80. window.addEventListener('resize', function () {
  81. myChart.resize()
  82. })
  83. }
  84. </script>
  85. <style scoped>
  86. .chart {
  87. height: 550px;
  88. }
  89. .detail-chart {
  90. height: 100%;
  91. overflow: auto;
  92. }
  93. </style>

OK,大功搞定!!!


本文转载自: https://blog.csdn.net/kang5789/article/details/140802269
版权归原作者 kang5789 所有, 如有侵权,请联系我们删除。

“echarts加载区域地图,并标注点”的评论:

还没有评论