2***@qq.com
2***@qq.com
  • 发布:2020-01-08 10:27
  • 更新:2025-08-25 17:54
  • 阅读:11454

uni-app map组件polyline 和markers 动态增加重绘问题

分类:uni-app

uni-app map组件使用polyline 和markers 动态增加多个,会发现一操作polyline 和markers对象都会重新渲染界面。
比例我先定义this.polyline = []
后面我增加this.polyline.push(....)画一条线,二条线
你会发觉每一次操作this.polyline对象都会新渲染界面,使得有很明显的界面闪动问题。

附视频(闪动问题)

3 关注 分享
zglszp 叶良书还我mx 632013207

要回复文章请先登录注册

1***@163.com

1***@163.com

回复 DCloud_UNI_yuhe :
你好 我将复现的代码放到这里了 上传了一个txt文件里面包含完整的代码 https://ask.dcloud.net.cn/question/213058?column=log&rf=false
2025-08-25 17:54
1***@163.com

1***@163.com

回复 DCloud_UNI_yuhe :
问题:定时更改marker标记点的rotate markers出现闪烁问题安卓手机 华为p70 微信小程序
我直接复制给你一个完整的代码demo 你放到uniapp里运行一下看看吧
<template>
<view>
<!-- 地图组件 -->
<map id="myMap" style="width: 100%; height: 100vh;" :latitude="centerLatitude" :longitude="centerLongitude"
:markers="markers" show-location>
<cover-view slot="callout">
<cover-view v-for="(item,index) in markers" :key="item.id">
<cover-view :marker-id="item.id">
<cover-view>{{item.title}}</cover-view>
<cover-image
src="https://img1.baidu.com/it/u=261583383,1036443967&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
style="width: 30px;height: 30px;border-radius: 50%;"></cover-image>
</cover-view>
</cover-view>
</cover-view>
</map>

<!-- 调试信息(可选) -->
<view class="debug-info">
<text>用户数量:{{ markers.length }}</text>
</view>
</view>
</template>
<script>
export default {
data() {
return {
centerLatitude: 39.908, // 地图中心纬度(可选)
centerLongitude: 116.397, // 地图中心经度(可选)
markers: [], // 存储所有用户的 Marker
userId: 'user1', // 当前用户 ID(模拟)
socketTask: null, // WebSocket 连接(模拟数据)
};
},
onLoad() {
//初始化模拟数据
this.initMockData();

// 定时更新位置(模拟实时数据)
setInterval(() => {
this.updateMarkersPosition();
}, 2000);
},
onUnload() {},
methods: {
// 初始化模拟数据
initMockData() {
// 模拟 3 个用户
const mockUsers = [{
id: 1,
name: '自己',
latitude: 39.908,
longitude: 116.397
},
{
id: 2,
name: '用户A',
latitude: 39.908,
longitude: 116.398
},
{
id: 3,
name: '用户B',
latitude: 39.909,
longitude: 116.397
},
];

// 转换为 markers 格式
this.markers = mockUsers.map(user => ({
id: user.id,
latitude: user.latitude,
longitude: user.longitude,
iconPath: 'https://img2.baidu.com/it/u=3190856383,2977784859&fm=253&fmt=auto&app=138&f=JPEG?w=285&h=285',
width: 20,
height: 20,
title: user.name,
anchor: {
x: 0.5,
y: 0.5
},
customCallout: { // 点击显示信息
display: 'ALWAYS',
anchorY: 0,
anchorX: 0,

}
}));
},

// 更新所有 Marker 的rotate
updateMarkersPosition() {
const list = [45, 60, 90, 120, 150, 270]
this.markers = this.markers.map(user => ({
id: user.id,
rotate: list[Math.floor(Math.random() * list.length)],
latitude: user.latitude,
longitude: user.longitude,
iconPath: user.iconPath,
anchor: user.anchor,
width: 20,
height: 20,
title: user.title,
customCallout: user.customCallout
}));

// 延迟 100ms 后执行移动动画(确保数据已更新)
setTimeout(() => {
this.moveAllMarkers();
}, 100);
},

// 移动所有 Marker(核心方法)
moveAllMarkers() {
const mapContext = uni.createMapContext('myMap', this);
// 遍历所有 Marker,只移动位置变化的
this.markers.forEach(marker => {
mapContext.translateMarker({
markerId: marker.id,
destination: {
latitude: marker.latitude,
longitude: marker.longitude
},
duration: 1000, // 1 秒移动完成
autoRotate: false, // 根据需求开启(如导航箭头)
animationEnd: () => {
console.log(`Marker ${marker.id} 已移动`);
}
});

});
}
}
};
</script>
2025-08-25 17:48
DCloud_UNI_yuhe

DCloud_UNI_yuhe

回复 1***@163.com :
能够提供一下一个可以复现的项目吗?
2025-08-25 16:47
1***@163.com

1***@163.com

请问解决了没 安卓闪烁ios没问题
2025-08-25 15:38
5***@qq.com

5***@qq.com

回复 用户2767450 :
我换高德地图使了
2025-08-07 16:40
用户2767450

用户2767450

2025年了,这个问题还没有解决吗
2025-05-09 15:08
qjr

qjr

回复 5***@qq.com :
兄弟,有解决这个问题了吗?
2024-09-14 09:42
5***@qq.com

5***@qq.com

回复 jgj_app :
哈哈哈 这个就是数据变化 丫的轨迹会全部重绘显示 中间就有个无到有的看着像闪烁一样 还没找到什么办法直接是接着轨迹线延长
2024-08-15 17:15
jgj_app

jgj_app

回复 5***@qq.com :
我是看第二页评论的
2024-08-15 17:12
5***@qq.com

5***@qq.com

回复 jgj_app :
谢谢 你这样解决了的吗? 我试了的 也不行
2024-08-15 17:10