使用高德地图的海量点绘制和区域绘制,区域绘制会因为海量点导致卡顿,有什么好的优化方法?
分批次绘制海量点,依旧会导致绘制区域的时候卡断
// 第六步,初始化地图
const BATCH_SIZE = 10; // 每批处理的标注数量
const BATCH_DELAY = 100; // 每批之间的延迟时间(毫秒)
function initMap() {
mountKey()
AMapLoader.load(loadOptions as any).then((AMap: any) => {
mapInstance = new AMap.Map('map-container', assignedConfig);
mapInstance.on('complete', () => {
getAdministrativeRegionInfo(AMap)
// 第七步, 添加矩形绘制工具
geometryConstructers.value.GeometryUtil = AMap.GeometryUtil;
const _MouseTool = new AMap.MouseTool(mapInstance);
geometryConstructers.value.MouseTool = _MouseTool;
// 第八步,监听摄像机列表变化,添加海量点标注
watch(() => tenantDeviceList.value, async (newVal, oldVal) => {
console.log('newVal :>> ', newVal);
if (newVal.length === 0) return
// 如果已存在图层,先移除
if (markersLayer) {
mapInstance.remove(markersLayer);
}
// 1、创建 AMap.LabelsLayer 图层
markersLayer = new AMap.LabelsLayer({
zooms: defaultSetting.zooms,
zIndex: 9999,
collision: true
});
//2、图层添加至地图实例
// mapInstance && mapInstance.add && mapInstance.add(markersLayer);
// 3、分批处理标注
const processMarkersBatch = async (startIndex: number) => {
const endIndex = Math.min(startIndex + BATCH_SIZE, newVal.length);
const batchMarkers = [] as any;
for (let i = startIndex; i < endIndex; i++) {
const obj = newVal[i];
let icon;
// 检查相同位置的设备
const sameLocationDevices = newVal.filter((item: any) =>
item.hyssDeviceEntity.latitude === obj.hyssDeviceEntity.latitude &&
item.hyssDeviceEntity.longitude === obj.hyssDeviceEntity.longitude
);
if (sameLocationDevices.length > 1) {
icon = collect_icon;
} else {
icon = obj.hyssDeviceEntity && obj.hyssDeviceEntity.deviceState === 1 ? online_icon : offline_icon;
}
let iconLable = obj?.hyssDeviceEntity?.deviceAddress || obj?.hyssDeviceEntity?.deviceName || '';
const markerObj = new AMap.LabelMarker({
extData: obj,
icon,
text: {
content: iconLable,
zooms: [12, 26],
style: {
padding: [1, 1, 1, 1],
fillColor: '#fff',
fontSize: 14,
strokeColor: '#000'
}
},
position: [obj?.hyssDeviceEntity?.longitude, obj?.hyssDeviceEntity?.latitude],
offset: new AMap.Pixel(0, 0),
anchor: 'bottom-center',
fitZoom: 11,
scaleFactor: 0.5,
maxScale: 2,
minScale: 0.5
});
batchMarkers.push(markerObj);
}
// 添加这一批标注
markersLayer && markersLayer.add && markersLayer.add(batchMarkers);
mapInstance && mapInstance.add && mapInstance.add(markersLayer);
// 如果还有剩余标注,继续处理下一批
if (endIndex < newVal.length) {
await new Promise(resolve => setTimeout(resolve, BATCH_DELAY));
await processMarkersBatch(endIndex);
}
};
// 开始处理第一批
await processMarkersBatch(0);
})
})
})
}
通过用户对地图的zoom去实现对多个相邻海量点 进行合并。 比如某个半径内有几十个小点,那就只渲染一个大的点。