处理聚合的代码
const instance = getCurrentInstance()
mapContext.value = uni.createMapContext("myMap",instance)
mapContext.value.initMarkerCluster({
enableDefaultStyle:false,
// gridSize:30,
success(res){
console.log(res,'聚合');
}
})
mapContext.value.on('markerClusterCreate',(e)=>{
// console.log("markerClusterCreate", e);
let clusterMarkers = []
const clusters = e.clusters // 新产生的聚合簇
clusters.forEach((cluster,index) => {
const {
center, // 聚合点的经纬度数组
clusterId, // 聚合簇id
markerIds // 已经聚合了的标记点id数组
} = cluster
let clusterObj = {
clusterId, //必须
...center,
width: 0,
height: 0,
iconPath: '/static/iconSvg/kong.png',
label: {// 定制聚合簇样式
content: markerIds.length + '家门店',
fontSize: 10,
color: '#fff',
width: 50,
height: 50,
bgColor: '#3687FC',
borderRadius: 25,
textAlign: 'center',
anchorX: -10,
anchorY: -35,
}
}
console.log(clusters);
clusterMarkers.push(clusterObj)
})
mapContext.value.addMarkers({
markers:clusterMarkers,
// clear:true,
})
})
处理坐标代码
const getAddressLowMarkers = (needCluster) => {
// if (markerCache.value.addressLow) return markerCache.value.addressLow;
const markers = address.value.map((p, i) => ({
id: Number(i) + 1000,
latitude: Number(p.latitude),
longitude: Number(p.longitude),
type: 'address',
joinCluster: needCluster, // 开启聚合
// #ifdef MP-WEIXIN
anchor: { x: 0.5, y: 0.3 },
// #endif
width: 50,
height: 50,
iconPath: "/static/iconSvg/beij.png",
label: {
// content: truncateMiddleAll(p.shopName, 5, 4, 0),
content:Number(i) + 1000,
color: '#fff',
fontSize: 10,
// #ifdef MP-WEIXIN
anchorX: p.shopName.length > 5 ? -20 : -(p.shopName.length * 5),
anchorY: model.value === 'ios' ? "-36rpx" : ""
// #endif
},
}));
// 过滤可见点后存入缓存
// const filtered = filterVisiblePoints(markers);
// markerCache.value.addressLow = filtered;
return markers;
};
5***@qq.com (作者)
升级到最新的版本了,还是没有聚合,没有聚合的坐标都是俩个相邻的,部分两个相邻的聚合了,两个以上也都聚合了,只有安卓真机有这个问题,谢谢回复
2025-09-03 15:41
DCloud_UNI_JBB
回复 5***@qq.com: 测试一下原生微信小程序是否有这个问题
2025-09-03 15:48
5***@qq.com (作者)
回复 DCloud_UNI_JBB: 原生的微信小程序开发者工具上没有这个问题
2025-09-03 16:02
DCloud_UNI_JBB
回复 5***@qq.com: 两边的代码保持一致了吗?
2025-09-03 16:04
5***@qq.com (作者)
回复 DCloud_UNI_JBB: 是的
2025-09-03 16:09
DCloud_UNI_JBB
回复 5***@qq.com: im里面私聊发我一个可复现demo,我试试
2025-09-03 16:13