当前位置:   article > 正文

【openlayers结合echarts图表和地图】VUE2 demo直接运行

openlayers结合
  1. <template>
  2. <div ref="mapContainer" class="map-content" />
  3. </template>
  4. <script>
  5. import 'ol/ol.css'
  6. import Map from 'ol/Map'
  7. import View from 'ol/View'
  8. import TileLayer from 'ol/layer/Tile'
  9. import XYZ from 'ol/source/XYZ'
  10. import GeoJSON from 'ol/format/GeoJSON'
  11. import VectorLayer from 'ol/layer/Vector'
  12. import VectorSource from 'ol/source/Vector'
  13. import EChartsLayer from 'ol-echarts'
  14. import Style from 'ol/style/Style'
  15. import Fill from 'ol/style/Fill'
  16. import Stroke from 'ol/style/Stroke'
  17. export default {
  18. mounted() {
  19. // 创建天地图瓦片图层
  20. const tileLayer = new TileLayer({
  21. source: new XYZ({
  22. url: 'http://t4.tianditu.com/DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=你的token'
  23. })
  24. })
  25. // 创建 GeoJSON 图层作为底图
  26. const vectorLayer = new VectorLayer({
  27. source: new VectorSource({
  28. url: '/static/map/china.json',
  29. format: new GeoJSON()
  30. }),
  31. style: function(feature) {
  32. return [
  33. new Style({
  34. fill: new Fill({
  35. color: 'rgb(255,255,255,0)' // 设置背景颜色
  36. }),
  37. stroke: new Stroke({
  38. color: '#5a8589', // 设置边框颜色
  39. width: 0.5
  40. })
  41. })
  42. ]
  43. }
  44. })
  45. // 添加注记
  46. const sourceCVA = new XYZ({
  47. url: 'http://t4.tianditu.com/DataServer?T=cva_w&x={x}&y={y}&l={z}&tk=你的token',
  48. crossOrigin: 'Anonymous',
  49. wrapX: false
  50. })
  51. const placeNameLayer = new TileLayer({
  52. source: sourceCVA
  53. })
  54. // 创建地图并设置图层
  55. this.map = new Map({
  56. target: this.$refs.mapContainer,
  57. layers: [tileLayer, vectorLayer, placeNameLayer],
  58. view: new View({
  59. center: [113.53450137499999, 34.44104525],
  60. projection: 'EPSG:4326',
  61. zoom: 5
  62. })
  63. })
  64. // 创建 ECharts 图层并添加到地图上
  65. const echartslayer = new EChartsLayer(this.createEChartsOption(), {
  66. hideOnMoving: false,
  67. hideOnZooming: false,
  68. forcedPrecomposeRerender: true
  69. })
  70. echartslayer.appendTo(this.map)
  71. },
  72. methods: {
  73. createEChartsOption() {
  74. return {
  75. title: {
  76. text: 'ECharts 在 OpenLayers 中的散点地图示例'
  77. },
  78. tooltip: {
  79. trigger: 'item'
  80. },
  81. legend: {
  82. // 设置图例的属性
  83. data: ['散点']
  84. },
  85. visualMap: {
  86. type: 'piecewise',
  87. splitNumber: 6, // 分段数量
  88. pieces: [
  89. { min: 0, max: 5, label: '0-5' },
  90. { min: 5, max: 10, label: '5-10' },
  91. { min: 10, max: 15, label: '10-15' },
  92. { min: 15, max: 20, label: '15-20' },
  93. { min: 20, max: 25, label: '20-25' },
  94. { min: 25, max: 30, label: '25-30' }
  95. ],
  96. calculable: true,
  97. inRange: {
  98. color: [
  99. '#FF0000',
  100. '#FF4500',
  101. '#FFA500',
  102. '#FFFF00',
  103. '#ADFF2F',
  104. '#32CD32'
  105. ] // 设置颜色范围
  106. }
  107. },
  108. series: [
  109. {
  110. name: '散点',
  111. type: 'scatter',
  112. symbolSize: 10, // 散点大小
  113. data: this.generateRandomData(30), // 生成30个随机数据点
  114. itemStyle: {
  115. emphasis: {
  116. borderColor: '#333',
  117. borderWidth: 1
  118. }
  119. }
  120. }
  121. ]
  122. }
  123. },
  124. generateRandomData(count) {
  125. const data = []
  126. for (let i = 0; i < count; i++) {
  127. data.push([
  128. 113 + Math.random() * 2, // 随机经度
  129. 34 + Math.random() * 2, // 随机纬度
  130. Math.floor(Math.random() * 31) // 随机数值,0到30之间
  131. ])
  132. }
  133. return data
  134. }
  135. }
  136. }
  137. </script>
  138. <style scoped>
  139. .map-content {
  140. width: 100%;
  141. height: 400px;
  142. }
  143. </style>

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/728134
推荐阅读
相关标签
  

闽ICP备14008679号