blur
当前位置:   article > 正文

openlayer展示热力图_openlayers热力图

openlayers热力图

 效果图:

一,安装openlayer

二,完整代码


 参考:openlayers6【十九】vue HeatmapLayer热力图层实现热力图效果详解_范特西是只猫的博客-CSDN博客_openlayers热力图实现

 效果图:

 在线预览:

WebGIS之家WebGIS之家,openlayers示例源码,cesium示例源码,openlayers在线调试预览,cesium在线调试预览,webgis,数字地球,数字孪生icon-default.png?t=N7T8https://www.webgishome.com/preview?id=71&example_name=heatmap&title=%E7%83%AD%E5%8A%9B%E5%9B%BE

一,安装openlayer

cnpm i -S ol

二,完整代码

  1. <!-- home -->
  2. <template>
  3. <div class="home">
  4. <div class="width:500px;height:300px">
  5. <span>blur</span>
  6. <el-slider v-model="option_heatmap.blur" show-input></el-slider>
  7. <span>radius</span>
  8. <el-slider v-model="option_heatmap.radius" show-input></el-slider>
  9. </div>
  10. <el-button @click="refreshData()">refreshData</el-button>
  11. <el-button @click="addHeatMapLayer()">addHeatMapLayer</el-button>
  12. <div id="map" class="content"></div>
  13. </div>
  14. </template>
  15. <script>
  16. import "ol/ol.css";
  17. import { Map, View } from "ol";
  18. import OSM from "ol/source/OSM";
  19. import VectorSource from "ol/source/Vector";
  20. import GeoJSON from "ol/format/GeoJSON";
  21. import { Heatmap as HeatmapLayer, Tile as TileLayer } from "ol/layer";
  22. export default {
  23. data() {
  24. return {
  25. map: {},
  26. option_heatmap: {
  27. blur: 43,
  28. radius: 50
  29. },
  30. heatMapLayer: {},
  31. heatData: {
  32. type: "FeatureCollection",
  33. features: [
  34. {
  35. type: "Feature",
  36. geometry: { type: "Point", coordinates: [112.4, 31.19] },
  37. properties: { weight: 0.9 }
  38. },
  39. {
  40. type: "Feature",
  41. geometry: { type: "Point", coordinates: [113.3, 30.6] },
  42. properties: { weight: 0.19 }
  43. },
  44. {
  45. type: "Feature",
  46. geometry: { type: "Point", coordinates: [123.3, 30.6] },
  47. properties: { weight: 0.419 }
  48. },
  49. {
  50. type: "Feature",
  51. geometry: { type: "Point", coordinates: [105.3, 30.6] },
  52. properties: { weight: 0.319 }
  53. },
  54. {
  55. type: "Feature",
  56. geometry: { type: "Point", coordinates: [106.3, 30.6] },
  57. properties: { weight: 0.719 }
  58. },
  59. {
  60. type: "Feature",
  61. geometry: { type: "Point", coordinates: [109.3, 31.6] },
  62. properties: { weight: 0.519 }
  63. },
  64. {
  65. type: "Feature",
  66. geometry: { type: "Point", coordinates: [109.3, 30.6] },
  67. properties: { weight: 0.319 }
  68. },
  69. {
  70. type: "Feature",
  71. geometry: { type: "Point", coordinates: [108.3, 32.6] },
  72. properties: { weight: 0.139 }
  73. },
  74. {
  75. type: "Feature",
  76. geometry: { type: "Point", coordinates: [118.3, 31.6] },
  77. properties: { weight: 0.129 }
  78. },
  79. {
  80. type: "Feature",
  81. geometry: { type: "Point", coordinates: [108.3, 33.6] },
  82. properties: { weight: 0.19 }
  83. },
  84. {
  85. type: "Feature",
  86. geometry: { type: "Point", coordinates: [108.3, 32.6] },
  87. properties: { weight: 0.189 }
  88. },
  89. {
  90. type: "Feature",
  91. geometry: { type: "Point", coordinates: [100.3, 30.6] },
  92. properties: { weight: 0.1 }
  93. }
  94. ]
  95. },
  96. newHeatData: {
  97. type: "FeatureCollection",
  98. features: [
  99. {
  100. type: "Feature",
  101. geometry: { type: "Point", coordinates: [112.4, 31.19] },
  102. properties: { weight: 0.9 }
  103. },
  104. {
  105. type: "Feature",
  106. geometry: { type: "Point", coordinates: [113.3, 30.6] },
  107. properties: { weight: 0.19 }
  108. },
  109. {
  110. type: "Feature",
  111. geometry: { type: "Point", coordinates: [123.3, 30.6] },
  112. properties: { weight: 0.419 }
  113. },
  114. {
  115. type: "Feature",
  116. geometry: { type: "Point", coordinates: [105.3, 30.6] },
  117. properties: { weight: 0.319 }
  118. }
  119. ]
  120. }
  121. };
  122. },
  123. components: {},
  124. created() {},
  125. mounted() {
  126. this.initMap();
  127. },
  128. computed: {},
  129. methods: {
  130. initMap() {
  131. this.map = new Map({
  132. target: "map",
  133. layers: [
  134. new TileLayer({
  135. source: new OSM()
  136. })
  137. ],
  138. view: new View({
  139. projection: "EPSG:4326", //使用这个坐标系
  140. center: [104.704968, 31.540962], //西南科技大学
  141. zoom: 5
  142. })
  143. });
  144. },
  145. addHeatMapLayer() {
  146. this.heatMapLayer = new HeatmapLayer({
  147. source: new VectorSource({
  148. features: new GeoJSON().readFeatures(this.heatData)
  149. }),
  150. blur: this.option_heatmap.blur,
  151. radius: this.option_heatmap.radius,
  152. weight: (e) => {
  153. return e.values_.weight;//根据权重展示热力图!关键点,weight范围为:0-1!!!
  154. },
  155. });
  156. this.map.addLayer(this.heatMapLayer);
  157. },
  158. refreshData() {
  159. this.heatData = this.newHeatData;
  160. this.map.removeLayer(this.heatMapLayer);
  161. this.addHeatMapLayer();
  162. }
  163. },
  164. watch: {
  165. option_heatmap: {
  166. handler(newVal, oldVal) {
  167. this.heatMapLayer.setBlur(this.option_heatmap.blur);
  168. this.heatMapLayer.setRadius(this.option_heatmap.radius);
  169. },
  170. deep: true
  171. }
  172. }
  173. };
  174. </script>
  175. <style scoped lang="scss">
  176. .home {
  177. height: 100vh;
  178. border: 1px solid red;
  179. .content {
  180. width: 800px;
  181. height: 600px;
  182. border: 1px solid blue;
  183. margin: auto;
  184. }
  185. }
  186. </style>

切记:这里实例化出来的heatmaplayer()里面一定要设置weight属性,并且这个属性的范围在【0,1】。这里封装了一下,可以把数组中的值映射为0到1,并返回新的数组,其思路主要是归一化,代码如下:

  1. test(arr) {
  2. var arr_max = Math.max.apply(null, arr);
  3. var arr_min = Math.min.apply(null, arr);
  4. //arr_num的每个值为0-1之间
  5. var arr_num = arr.map((item) => {
  6. return (item - arr_min) / (arr_max - arr_min);
  7. });
  8. return arr_num;
  9. },

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