uniapp中map使用点聚合渲染marker覆盖物

效果如图:

一、什么是点聚合

当地图上需要展示的标记点 marker 过多时,可能会导致界面上 marker 出现压盖,展示不全,并导致整体性能变差。针对此类问题,推出点聚合能力。
点聚合官网教程

二、基本用法

  • template
html 复制代码
<map id="map" :latitude="latitude" :longitude="longitude" :style="{ width: '100%', height: windowHeight + 'px' }" :scale="10">
  • onReady
js 复制代码
// 获取屏幕高度
		uni.getSystemInfo({
			success: (res) => {
				this.windowHeight = res.windowHeight;
			},
		});
		
		// 创建map对象
		this._mapContext = uni.createMapContext("map", this);
		this.cluster();
  • data
js 复制代码
			_mapContext: null,
			windowHeight: 0,
			latitude: 23.099994,
			longitude: 113.324520,
  • methods
js 复制代码
		// 点聚合
		cluster() {
			// 仅调用初始化,才会触发 on.("markerClusterCreate", (e) => {})
			this._mapContext.initMarkerCluster({
				enableDefaultStyle: false, // 是否使用默认样式
				zoomOnClick: true, // 点击聚合的点,是否改变地图的缩放级别
				gridSize: 60, // 聚合计算时网格的像素大小,默认60
				complete(res) {
					console.log('initMarkerCluster', res)
				}
			});

			this._mapContext.on("markerClusterCreate", (res) => {
				console.log("markerClusterCreate", res);
				const clusters = res.clusters
				const markers = clusters.map(cluster => {
					const {
						center,
						clusterId,
						markerIds
					} = cluster
					return {
						...center,
						width: 0,
						height: 0,
						clusterId, // 必须
						label: {
							content: markerIds.length + '',
							fontSize: 16,
							width: 50,
							height: 50,
							bgColor: '#00A3FA',
							borderRadius: 25,
							textAlign: 'center',
							anchorX: 0,
							anchorY: -20,
						}
					}
				})
				this._mapContext.addMarkers({
					markers,
					clear: false,
					complete(res) {
						console.log('clusterCreate addMarkers', res)
					}
				})
			});
			this._mapContext.on('markerClusterClick', (res) => {
				console.log('markerClusterClick', res)
			})
			this.addMarkers();
		},

		// 添加标记点
		addMarkers() {
			const positions = [
				{
					latitude: 23.099994,
					longitude: 113.324520,
				}, {
					latitude: 23.099994,
					longitude: 113.322520,
				}, {
					latitude: 23.099994,
					longitude: 113.326520,
				}, {
					latitude: 23.096994,
					longitude: 113.329520,
				}
			]

			const markers = []
			positions.forEach((p, i) => {
				markers.push(
					Object.assign({}, {
						id: i + 1,
						iconPath: "/static/images/map0.png",
						width: 28,
						height: 29,
						joinCluster: true, // 指定了该参数才会参与聚合
						callout:{
							bgColor: "#5AC2EB",
							color: "#fff",
							content: "客户名称",
							display: "ALWAYS",
							fontSize: "14",
							fontWeight: "bold",
							padding: 8,
							textAlign: "center"
						}
					}, p)
				)
			})
			console.log('markers', markers)
			this._mapContext.addMarkers({
				markers,
				clear: false,
				complete(res) {
					console.log('addMarkers', res)
				}
			})
		},

三、踩坑总结

1. 调用initMarkerCluster方法,否则不生效

js 复制代码
			// 创建map对象
			this._mapContext = uni.createMapContext("map", this);
			// 仅调用初始化,才会触发 on.("markerClusterCreate", (e) => {})
			this._mapContext.initMarkerCluster({
				enableDefaultStyle: false, // 是否使用默认样式
				zoomOnClick: true, // 点击聚合的点,是否改变地图的缩放级别
				gridSize: 60, // 聚合计算时网格的像素大小,默认60
				complete(res) {
					console.log('initMarkerCluster', res)
				}
			});

2. joinCluster属性设为true,否则不生效

marker对象上必须有该属性

3. 确保每个marker有唯一的id,否则可能会出问题

4. 安卓和ios兼容问题

1. iconPath不要有中文,安卓和微信小程序可以使用中文,但苹果端使用中文要出异常,所以统一不要使用中文

2.小程序--安卓端直接传入marker数组无法使用点聚合的效果. 小程序--苹果端通过调用addMarkers无法使用点聚合的效果. 因此,此处必须写兼容代码分别处理

  • 安卓
js 复制代码
		this._mapContext.addMarkers({
				markers,
				clear: false,
				complete(res) {
					console.log('addMarkers', res)
				}
			})
  • ios
html 复制代码
<map id="map" :latitude="latitude" :longitude="longitude" :style="{ width: '100%', height: windowHeight + 'px' }" :scale="10" markers="markerList">
相关推荐
计算机学姐1 天前
基于微信小程序的校园失物招领管理系统【uniapp+springboot+vue】
java·vue.js·spring boot·mysql·信息可视化·微信小程序·uni-app
2501_915921431 天前
HTTPS前端劫持 新一代流量劫持解决方案
前端·网络协议·ios·小程序·https·uni-app·iphone
爱怪笑的小杰杰1 天前
优化 UniApp 日历组件的多语言切换:告别 setLocale 引起的 App 重启
java·前端·uni-app
计算机学姐1 天前
基于微信小程序的宠物服务系统【uniapp+springboot+vue】
java·vue.js·spring boot·mysql·微信小程序·uni-app·宠物
2501_915909061 天前
iOS应用签名的三种方法全解析:从官方到第三方工具
android·ios·小程序·https·uni-app·iphone·webview
心中无石马2 天前
uniapp引入tailwindcss4.x
前端·css·uni-app
fix一个write十个2 天前
【uniApp开发】微信小程序 web-view 内嵌 H5 跳转支付踩坑实录
微信小程序·uni-app
wuxianda10302 天前
苹果App上架4.3a被拒解决方案汇报总结
ios·uni-app·objective-c·cocoa·苹果上架·4.3a
西洼工作室2 天前
uniapp+vue3+python对接阿里云短信认证服务alibabacloud_dypnsapi20170525
python·阿里云·uni-app
wuxianda10302 天前
苹果App上架4.3a问题3天解决方案汇报总结
开发语言·javascript·uni-app·ecmascript·ios上架·苹果上架