quanyawei
2024-11-15 f752f50a484f63fc3786ab1c7ad563f3b17cce77
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<template>
  <div id="container" style="width:100%;height:100%" />
</template>
 
<script>
// 这里可以导入其他文件(比如:组件,工具js,第三方插件js,json文件,图片文件等等)
// 例如:import《组件名称》from'《组件路径》';
import $ from 'jquery'
export default {
// import 引入的组件需要注入到对象中才能使用
  components: {},
  props: {},
  data() {
    // 这里存放数据
    return {
 
    }
  },
  // 计算属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {},
  // 生命周期 - 创建完成(可以访问当前 this 实例)
  created() {
  },
  // 生命周期 - 挂载完成(可以访问 DOM 元素)
  mounted() {
    this.init()
  },
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {},
  // 方法集合
  methods: {
    init() {
      var map = new AMap.Map('container', {
        mapStyle: 'amap://styles/db9efe6a1745ac24b7269b862f359536',
        zoom: 9,
        center: [120.198254, 36.465551],
        zooms: [4, 12],
        pitch: 46,
        viewMode: '3D'
      })
 
      $.get('//a.amap.com/Loca/static/mock/qingdao_500m.tsv', function(heatmapData) {
        var layer = new Loca.HexagonLayer({
          map: map,
          fitView: true,
          eventSupport: true
        })
 
        layer.setData(heatmapData, {
          lnglat: function(obj) {
            var val = obj.value
            return [val['lng'], val['lat']]
          },
          value: 'count',
          type: 'tsv'
        })
 
        layer.setOptions({
          unit: 'meter',
          mode: 'count',
          style: {
            color: ['#4575b4', '#99d594', '#e6f598', '#ffffbf', '#fee08b', '#fee08b', '#d53e4f'],
            radius: 1000,
            opacity: 1,
            gap: 300,
            height: [0, 11500]
          }
        })
 
        // 事件 legendupdate: 图例数据更新完成回调此函数
        layer.on('legendupdate', function(ev) {
          var colorLegend = ev.colorLegend
          initLegend(colorLegend)
        })
 
        // 鼠标事件,鼠标移动,更新数据
        // layer.on('mousemove', function(ev) {
        //   updateInfo(ev)
        // })
 
        layer.render()
 
        // 鼠标事件更新数据
        function updateInfo(ev) {
          var $val = document.getElementById('val')
          var $idx = document.getElementById('indexes')
          var $num = document.getElementById('indexes-num')
          var $lngLat = document.getElementById('lng-lat')
 
          $val.innerText = ev.value
          $idx.innerText = ev.indexes.join(', ')
          $num.innerText = ev.indexes.length
          $lngLat.innerText = ev.lngLat.valueOf()
        }
 
        // 更新图例
        function initLegend(colorLegend) {
          var legends = colorLegend.map(item => {
            // color 为 gradient 传入的格式
            return `<li class="color-item" style="background-color: ${item.color}"></li>`
          })
 
          var ranges = colorLegend.map((item, index) => {
            // range 可能为小数,可以自行取整计算
            item.range[0] = Math.round(item.range[0])
            item.range[1] = Math.round(item.range[1])
 
            if (index === colorLegend.length - 1) {
              return `<li class="label-item">${item.range[0]}</li><li class="label-item">${item.range[1]}</li>`
            }
            return `<li class="label-item">${item.range[0]}</li>`
          })
 
          // document.getElementById('legend-color').innerHTML = legends.join('')
          // document.getElementById('legend-label').innerHTML = ranges.join('')
        }
      })
    }
  } // 如果页面有keep-alive缓存功能,这个函数会触发
}
</script>
<style scoped>
/* @import "//a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css"; */
 
.info span {
    min-width: 300px;
    max-width: 400px;
    color: #1b91ff;
}
.legend {
    position: absolute;
    bottom: 20px;
    right: 20px;
}
 
.legend ul {
    padding: 0;
    margin: 0;
    list-style: none;
}
 
.color-item {
    width: 30px;
    height: 20px;
    display: inline-block;
}
 
.label-item {
    display: inline-block;
    width: 30px;
    margin-left: -2px;
    font-size: 14px;
}
</style>