uniapp map地图实现marker聚合点,并点击marker触发事件

1.uniapp官方文档说明

2.关键代码片段

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

3.全部代码

javascript 复制代码
<template>
	<view class="content">
		<map id="map" :latitude="latitude" :longitude="longitude"
			:style="{ width: '100%', height: windowHeight + 'px' }" :scale="10" @markertap="markTap"></map>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				_mapContext: null,
				windowHeight: 0,
				latitude: 23.099994,
				longitude: 113.324520,
			}
		},
		onLoad() {

		},
		onReady() {
			uni.getSystemInfo({
				success: (res) => {
					this.windowHeight = res.windowHeight;
				},
			});

			// 创建map对象
			this._mapContext = uni.createMapContext("map", this);
			this.cluster();
		},
		methods: {
			markTap(e) {
				console.log('ccccccc')
				console.log(e)
				console.log('ccccccc')
				uni.showToast({
					title: `客户名称${e.markerId}`
				})
			},
			// 点聚合
			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,
								color: '#ffffff',
								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,
					id: 12
				}, {
					latitude: 23.099994,
					longitude: 113.322520,
					id: 13
				}, {
					latitude: 23.099994,
					longitude: 113.326520,
					id: 14
				}, {
					latitude: 23.096994,
					longitude: 113.329520,
					id: 15
				}]

				const markers = []
				positions.forEach((p, i) => {
					markers.push(
						Object.assign({}, {
							id: p.id,
							iconPath: "/static/pile.png",
							width: 28,
							height: 29,
							joinCluster: true, // 指定了该参数才会参与聚合
							callout: {
								bgColor: "#5AC2EB",
								color: "#fff",
								content: `充电桩${p.id}`,
								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)
					}
				})
			},
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>

3.效果

相关推荐
草字3 小时前
uniapp 防止长表单数据丢失方案,缓存表单填写内容,放置卡退或误操作返回。
前端·javascript·uni-app
2501_915106324 小时前
JavaScript编程工具有哪些?老前端的实用工具清单与经验分享
开发语言·前端·javascript·ios·小程序·uni-app·iphone
2501_916013745 小时前
iOS 上架 App 全流程实战,应用打包、ipa 上传、App Store 审核与工具组合最佳实践
android·ios·小程序·https·uni-app·iphone·webview
2501_915106325 小时前
iOS 26 能耗监测全景,Adaptive Power、新电池视图
android·macos·ios·小程序·uni-app·cocoa·iphone
BumBle8 小时前
uniapp 用css实现圆形进度条组件
前端·vue.js·uni-app
iOS阿玮15 小时前
分享一个4.3(a)瓜,一个操作毁了公司3个月的成果。
uni-app·app·apple
00后程序员张16 小时前
苹果软件混淆的工程逻辑,从符号空间到资源扰动的体系化实现
android·ios·小程序·https·uni-app·iphone·webview
街尾杂货店&21 小时前
word属性解释
uni-app
笨笨狗吞噬者1 天前
【uniapp】小程序体积优化,JSON文件压缩
前端·微信小程序·uni-app
源码集结号1 天前
一套智慧工地云平台源码,支持监管端、项目管理端,Java+Spring Cloud +UniApp +MySql技术开发
java·mysql·spring cloud·uni-app·源码·智慧工地·成品系统