uni-app renderjs渲染失败性能瓶颈

uni-app renderjs渲染失败性能瓶颈

示例代码:

<template>
<view class="page">
<div id="amap" class="map"></div>
</view>
</template>
<script>
export default {
data() {
return {};
},
onShow() {},
mounted() {},
onLoad() {},
methods: {},
};
</script>
<script module="amap" lang="renderjs">
let myChart
export default {
data() {
return {
map:null,
myData:[],
};
},
mounted() {
this.init()
},
methods: {
init() {
console.log(2)
const script = document.createElement('script')
script.src = '离线地图JS地址'
script.onload = this.initAmap.bind(this)
document.head.appendChild(script)
console.log('eles');
},
//初始化地图
initAmap() {
console.log('初始化')
this.map = new AMap.Map('amap', {
resizeEnable: true,
center: [116.397428, 39.90923],
zooms: [4, 20], //设置地图级别范围
zoom: 18
})
this.map.on('complete', () => {
console.log('加载完成');
})
this.getItem(this.myData)
},
// 给地图绘制点 Makers
addMaker(item) {
let marker = new AMap.Marker({
//经纬度位置
position: new AMap.LngLat(item.longitude, item.latitude),
//便宜量
offset: new AMap.Pixel(-10, -24),
//图标
icon: new AMap.Icon({
//大小
size: new AMap.Size(20, 25),
imageSize: new AMap.Size(20, 25),
image: 'imgpath'
}),
//图标展示层级,防止被隐藏时编写
zIndex: 100,
//图标旁边展示内容
label: {
content: `<view>content</view>`,
offset: new AMap.Pixel(10, -18)
}
})
//给图标添加点击事件
marker.on('click', (e) => {
console.log(item, e);
})
//将图标添加到地图中
this.map.add(marker)
},
//绘制点与点之间的线段 Polyline类
initLine(start, end) {
let polyline = new AMap.Polyline({
//线段粗细
strokeWeight: 5,
//颜色
strokeColor: '#007AFF',
//形状
lineCap: 'round',
lineJoin: 'round',
//是否显示方向
showDir: true,
//起点与终点经纬度[[longitudeStart,latitudeStart],[longitudeEnd,latitudeEnd]]
path: [start, end]
})
//向地铁图添加线段
this.map.add(polyline)
},
}
</script>
<style lang="scss" scoped>
amap {
width: 600px;
height: 600px;
}
</style>

操作步骤:

运行如上代码,需要注意的是我司使用的是高德内网地图服务,会同时加载二十余个地图瓦片

预期结果:

连续报错Too many active WebGL contexts

实际结果:

连续报错Too many active WebGL contexts

bug描述:

目前怀疑web-view性能过低,无法同时加载多个资源,导致高德地图渲染失败

信息 内容
产品分类 uniapp/App
PC开发环境 Windows
PC操作系统版本 11
HBuilderX类型 正式
HBuilderX版本 4.56
手机系统 Android
手机系统版本 Android 16
手机厂商 华为
手机机型 MHA-AL00
页面类型 vue
vue版本 vue2
打包方式 云端
项目创建方式 HBuilderX

更多关于uni-app renderjs渲染失败性能瓶颈的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于uni-app renderjs渲染失败性能瓶颈的实战教程也可以访问 https://www.itying.com/category-93-b0.html


这是一个典型的WebGL上下文过多导致的性能问题。在uni-app中使用renderjs渲染高德地图时,每个地图实例都会创建一个WebGL上下文,而浏览器对同时活跃的WebGL上下文数量有限制(通常6-16个)。

主要问题分析:

  1. WebGL上下文泄露:每次创建地图实例时没有正确销毁之前的上下文
  2. 内存管理不当:地图瓦片和标记没有及时清理
  3. renderjs生命周期管理:缺少组件销毁时的资源释放

建议优化方案:

// 在renderjs中添加销毁方法
methods: {
  destroyMap() {
    if (this.map) {
      this.map.destroy();
      this.map = null;
    }
  }
},

// 在beforeDestroy或onUnload中调用
beforeDestroy() {
  this.$refs.amap.destroyMap();
}
回到顶部