10Heatmap.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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>ol热力图渲染能力测试</title>
  7. <link rel="stylesheet" href="../dist/ol/ol.css" />
  8. <script src="../dist/index.js"></script>
  9. <script src="https://unpkg.com/ol@7.4.0/dist/ol.js"></script>
  10. <script src="https://unpkg.com/vue@2"></script>
  11. <style>
  12. body {
  13. margin: 0
  14. }
  15. #map {
  16. height: 100vh;
  17. }
  18. .control {
  19. position: absolute;
  20. top: 20px;
  21. left: 40px;
  22. background: #fff
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <div id="app">
  28. <div id="map"></div>
  29. <div class="control">
  30. <input type="number" min="0" v-model="featuresCount" @keyup.enter="setHeatmapFeatures">万
  31. <button @click="setHeatmapFeatures">渲染热力图</button>
  32. <labal>渲染时间:<span v-if="renderEnd">{{renderEnd - renderBegin}}</span>ms</labal>
  33. </div>
  34. </div>
  35. </body>
  36. <script>
  37. const source = new CTMapOl.source.XYZ({
  38. url: 'http://t0.tianditu.gov.cn/vec_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=vec&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=86db5390161c4da2abe96c3fca00f403'
  39. })
  40. const tileLayer = new CTMapOl.layer.Tile({
  41. title: '天地图',
  42. source: source
  43. })
  44. var sourceMarker = new CTMapOl.source.XYZ({
  45. url: 'http://t0.tianditu.gov.cn/cva_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cva&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=86db5390161c4da2abe96c3fca00f403'
  46. })
  47. const tileMarker = new CTMapOl.layer.Tile({
  48. title: '标注图层',
  49. source: sourceMarker
  50. })
  51. new Vue({
  52. el: '#app',
  53. data () {
  54. return {
  55. featuresCount: 1,
  56. renderBegin: null,
  57. renderEnd: null
  58. }
  59. },
  60. mounted () {
  61. this.map = new CTMapOl.Map({
  62. view: new CTMapOl.View({
  63. projection: 'EPSG:4326',
  64. center: [116.39, 39.91],
  65. zoom: 17,
  66. maxZoom: 18,
  67. minZoom: 1
  68. }),
  69. layers: [tileLayer, tileMarker],
  70. target: 'map'
  71. })
  72. this.initHeatmap()
  73. this.setHeatmapFeatures()
  74. },
  75. methods: {
  76. initHeatmap () {
  77. const heatSource = this.heatSource = new ol.source.Vector()
  78. const heatLayer = new ol.layer.Heatmap({
  79. source: heatSource
  80. })
  81. this.map.addLayer(heatLayer)
  82. heatLayer.addEventListener('prerender', e => {
  83. this.renderEnd = null
  84. this.renderBegin = new Date().getTime()
  85. })
  86. heatLayer.addEventListener('postrender', e => {
  87. this.renderEnd = new Date().getTime()
  88. })
  89. },
  90. setHeatmapFeatures () {
  91. this.heatSource.clear()
  92. const features = []
  93. for (let i = 0; i < this.featuresCount * 10000; i++) {
  94. const latitude = Math.random() / 100 + 39.905
  95. const longitude = Math.random() / 100 + 116.385
  96. const point = [longitude, latitude]
  97. features[i] = new ol.Feature({
  98. geometry: new ol.geom.Point(point)
  99. })
  100. }
  101. this.heatSource.addFeatures(features)
  102. }
  103. }
  104. })
  105. </script>
  106. </html>