将接口返回的经纬集合点在高德地图上标记展示,

如果实时刷新地图标记点,不加优化,则会造成过多的带宽消耗

所以,地图只需加载一次,局部更新标记点就好了

代码:

<template>
<section class="map_warpper">
<div id="container" class="map"></div>
</section>
</template> <script>
import redMarker from '@/assets/img/marker-red.png'
import locationMarker from '@/assets/img/marker-location.png' export default {
name: 'mapContainer',
data () {
return {
    // 动态的经纬度点集合
locationArray: [],
markers: [],
cluster: [],
map: null,
// 地图中心点
regionLocation: [],
zoom: 0
}
},
mounted () {
this.initMap()
},
methods: {// 实例化地图
initMap () {
this.regionLocation = util.cookies.get('regionLocation').split(',')
const AMap = window.AMap
this.map = new AMap.Map('container', {
resizeEnable: true,
center: this.regionLocation,
zoom: 12
})
  this.setMarker()
},
// 设置点标记
setMarker () {
this.markers = []
const AMap = window.AMap
// 标记样式
for (const i in this.locationArray) {
const position = this.locationArray[i].split(',')
this.markers.push(new AMap.Marker({
position: position,
content: '<img src=' + locationMarker + '>',
offset: new AMap.Pixel(-15, -15)
}))
}
// 聚合点样式
let sts = [{
url: redMarker,
size: new AMap.Size(64, 64),
offset: new AMap.Pixel(-32, -32)
}]
// 点聚合
this.cluster = new AMap.MarkerClusterer(this.map, this.markers, { styles: sts, gridSize: 80 })
// 自适应展示所有标记点
this.map.setFitView(this.markers)
this.zoom = this.map.getZoom()
}
}
}
</script> <style>
.map {
width: 100%;
height: 100%;
}
.map_warpper {
width: 100%;
height: 800px;
}
</style>

上面代码解决了两个bug:

1.每次执行setMarker()时,都会this.markers = [],

这一步清除标记点,驱动地图标记刷新,然而并未刷新,新旧点并存,随着叠加,点会越来越多,

这时记得queryLocation()调用前强行删除点 this.cluster.Ra = []

2.然而集合点更新了,但视图依旧未触发更新,这时就需要缩放地图来触发更新,

this.map.getZoom()获取当前缩放级别,然后通过 this.map.setZoom()设置缩放,视图达到刷新,问题解决

最新文章

  1. Druid初步学习
  2. 从网易与淘宝的font-size思考前端设计稿与工作流 (转)
  3. C++调用C函数
  4. 观看github前100开源项目的读后感
  5. POJ 2513 Colored Sticks
  6. 二叉树建立,先序、中序、后序遍历(c实现)
  7. updmap-sys failed. Output has been stored in
  8. 【Todo】【转载】ES6的学习记录
  9. Python 线程,进程
  10. java选择结构
  11. 02-Centos7安装部署Mirrorgate
  12. PHP之Boolean
  13. SpringMVC防止表单重复提交
  14. WDA-Webdynpro应用发布至EP
  15. Linux强制杀进程命令行工具
  16. Mongoose也是个大坑
  17. 实验3 --俄罗斯方块 with 20135335郝爽
  18. js点击复制功能的实现
  19. 跟着百度学PHP[14]-PDO之Mysql的事务处理2
  20. out 和ref 的区别

热门文章

  1. JavaScript JSON 与 AJAX
  2. Spring核心组件知识梳理
  3. Mac word文档的消失问题以及解决方案
  4. 个人项目:WordCount (Java)
  5. python 报错:a bytes-like object is required, not &#39;str&#39;
  6. 如何让Java应用成为杀不死的小强?(中篇)
  7. 1006 Sign In and Sign Out (25 分)
  8. docker中使用mysql数据库详解(在局域网访问)
  9. PTA | 1008 数组元素循环右移问题 (20分)
  10. Flask 入门(十)