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

news/2025/2/20 9:48:29/

1.uniapp官方文档说明
在这里插入图片描述
在这里插入图片描述

2.关键代码片段

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

3.全部代码

<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, // 聚合计算时网格的像素大小,默认60complete(res) {console.log('initMarkerCluster', res)}});this._mapContext.on("markerClusterCreate", (res) => {console.log("markerClusterCreate", res);const clusters = res.clustersconst markers = clusters.map(cluster => {const {center,clusterId,markerIds} = clusterreturn {...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.效果
在这里插入图片描述

在这里插入图片描述


http://www.ppmy.cn/news/1168678.html

相关文章

React Hooks批量更新问题

React 版本17.0.2 import React, { useState } from react;const Demo () > {const [count, setCount] useState(0);const [count1, setCount1] useState(0);const [count2, setCount2] useState(0);console.log(Demo);const add async () > {await 10;setCount(c…

SQL INSERT INTO 语句(在表中插入)

SQL INSERT INTO 语句 INSERT INTO 语句用于向表中插入新的数据行。 SQL INSERT INTO 语法 INSERT INTO 语句可以用两种形式编写。  第一个表单没有指定要插入数据的列的名称&#xff0c;只提供要插入的值&#xff0c;即可添加一行新的数据&#xff1a; INSERT INTO table_n…

IIC基础知识

文章目录 寻址方式 寻址方式

Linux/Ubuntu 安装 Java运行环境

linux下安装Java运行环境 1、下载安装包 .tar.gz 先在官网下载 JDK 点击这里 在这里要选择对应的 JDK 版本&#xff0c;一般我们目前选择JDK8 点击这里 2、在 /usr/local/ 目录下创建Java文件夹 cd /usr/local/ mkdir java3、将下载的文件通过FTP程序上传到刚刚创建的Java文…

网络协议--RARP:逆地址解析协议

5.1 引言 具有本地磁盘的系统引导时&#xff0c;一般是从磁盘上的配置文件中读取IP地址。但是无盘机&#xff0c;如X终端或无盘工作站&#xff0c;则需要采用其他方法来获得IP地址。 网络上的每个系统都具有唯一的硬件地址&#xff0c;它是由网络接口生产厂家配置的。无盘系统…

java基础--transient关键字减少序列化

前置内容 1.序列化 今天在看ArrayList源码的时候看到了这个&#xff0c;我之前应该是看过的&#xff0c;但是忘记了。现在在总结一下。 transient 用在类的属性上&#xff0c;不能修饰其他的。 作用&#xff1a;在序列化的时候transient修饰的属性不能被序列化 用途 在一些…

蓝桥杯 (猜生日、棋盘放麦子、MP3储存 C++)

思路&#xff1a; 1、用循环。 2、满足条件&#xff0c;能整除2012、3、12且month等于6、day<30 #include<iostream> using namespace std; int main() {for (int i 19000101; i < 20120312; i){int month i / 100 % 100;int day i % 100;if (i % 2012 0 &…

js鼠标点击添加图标并获取图标的坐标值

给这个图片添加摄像头图标&#xff0c;并获取图标的坐标值&#xff0c;也就是图标的css样式是positon:absolute,获取left和top的值。 图片1 思路是这样的&#xff0c;获取这里的长度&#xff0c; 图片2 1.鼠标点击时距浏览器的左边距离和上边距离&#xff0c;相当于(0,0)坐标 …