高德局部刷新标记点,bug解决
2024-09-07 08:52:49
将接口返回的经纬集合点在高德地图上标记展示,
如果实时刷新地图标记点,不加优化,则会造成过多的带宽消耗
所以,地图只需加载一次,局部更新标记点就好了
代码:
<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()设置缩放,视图达到刷新,问题解决
最新文章
- Druid初步学习
- 从网易与淘宝的font-size思考前端设计稿与工作流 (转)
- C++调用C函数
- 观看github前100开源项目的读后感
- POJ 2513 Colored Sticks
- 二叉树建立,先序、中序、后序遍历(c实现)
- updmap-sys failed. Output has been stored in
- 【Todo】【转载】ES6的学习记录
- Python 线程,进程
- java选择结构
- 02-Centos7安装部署Mirrorgate
- PHP之Boolean
- SpringMVC防止表单重复提交
- WDA-Webdynpro应用发布至EP
- Linux强制杀进程命令行工具
- Mongoose也是个大坑
- 实验3 --俄罗斯方块 with 20135335郝爽
- js点击复制功能的实现
- 跟着百度学PHP[14]-PDO之Mysql的事务处理2
- out 和ref 的区别
热门文章
- JavaScript JSON 与 AJAX
- Spring核心组件知识梳理
- Mac word文档的消失问题以及解决方案
- 个人项目:WordCount (Java)
- python 报错:a bytes-like object is required, not &#39;str&#39;
- 如何让Java应用成为杀不死的小强?(中篇)
- 1006 Sign In and Sign Out (25 分)
- docker中使用mysql数据库详解(在局域网访问)
- PTA | 1008 数组元素循环右移问题 (20分)
- Flask 入门(十)