当前位置: 首页 > news >正文

uniapp小程序自定义聚合点

注:

1.默认的聚合点可以点击自动展示子级点位,但是自定义的聚合点在ios上无法触发markerClusterClick的监听,至今未解决,不知啥原因

2.ios和安卓展示的点位样式还有有差别

源码附上

<template><view class="marker-map"><map id="mapId" style="width: 100%; height:1342rpx ;" :latitude="latitude" :longitude="longitude"show-location show-scale :scale="zoom" @regionchange="regionchange" @updated="update" @callouttap='callouttap'></map><view class="layer flex-column align-end"><view class="cur-location justify-center align-center" @click="backCenter"><image :src="imgConf.position" mode="" style="width:60%;height: 60%;"></image></view><view class="list"><u-transition :show="showMarkerListPop" mode="fade-up"><mapMarkerList @onclose="showMarkerListPop=false" /></u-transition></view></view></view>
</template><script>import mapMarkerList from './components/mapMarkerList.vue'import imgConf from '@/utils/imgConf.js'export default {components: {mapMarkerList},data() {return {imgConf,longitude: 104.04311,latitude: 30.64242,markers: [], // 使用 marker点击事件 需要填写idfixedHeight: 60,zoom: 16,showMarkerListPop: false}},created() {this.mapContext = uni.createMapContext("mapId", this);console.log("this.mapContext", this.mapContext);// 使用默认聚合效果时可注释下一句this.bindEvent()this.getDotList();},methods: {bindEvent() {this.mapContext.initMarkerCluster({enableDefaultStyle: false,zoomOnClick: true,gridSize: 100,complete(res) {console.log('initMarkerCluster', res)}});// enableDefaultStyle 为 true 时不会触发该事件this.mapContext.on('markerClusterCreate', res => {const clusters = res.clustersconst markers = clusters.map(cluster => {const {center,clusterId,markerIds} = clusterreturn {...center,width: 1,height: 1,clusterId, // 必须iconPath: '',label: {content: '盛世年华' + "(" + "2" + "套)" + '\n' + '12' + '元起',color: "#ffffff",fontSize: 11,borderRadius: 8,bgColor: "#0090FF",padding: 10,textAlign: 'center',anchorX: 0,anchorY: -56,}// callout: {// 	content: '盛世年华' + "(" + "2" + "套)" + '\n' + '12' + '元起',// 	color: "#ffffff",// 	fontSize: 11,// 	borderRadius: 8,// 	bgColor: "#0090FF",// 	padding: 10,// 	textAlign: 'center',// 	display: 'ALWAYS',// },}})this.mapContext.addMarkers({markers,clear: false,complete(res) {console.log('clusterCreate addMarkers', res)}})})this.mapContext.on("markerClusterClick", (ClusterInfo) => {console.log("点击聚合点", ClusterInfo); //[id1,id2]});},// 获取所有点位/async getDotList(params) {// const res = await getDot(params)const resPosition = [{longitude: 113.324520,latitude: 23.099994,title: '盛世年华1',num: 1,price: 35500},{longitude: 102.04320,latitude: 31.64242,title: '盛世年华2',num: 1,price: 35500},{longitude: 102.04313,latitude: 30.64242,title: '盛世年华3',num: 1,price: 15500},{longitude: 113.326520,latitude: 31.64242,title: '盛世年华4',num: 3,price: 25500},]const markers = []resPosition.forEach((p, i) => {markers.push(Object.assign({}, {id: i + 1,iconPath: '',joinCluster: true,width: 2,height: 2,callout: {content: p.title + "(" + p.num + "套)" + '\n' + p.price + '元起',color: "#ffffff",fontSize: 11,borderRadius: 8,bgColor: "#0090FF",padding: 10,textAlign: 'center',display: 'ALWAYS',},}, p))})this.markers = JSON.parse(JSON.stringify(markers))this.mapContext.addMarkers({markers: this.markers,clear: false,complete(res) {console.log('addMarkers', res)}})// .map((item, index) => {// 	if (item.lnglat === '') {// 		return undefined// 	}// 	var location = item.x.split(",");// 	let iconPath = ''// 	let label = item.name.length > 20 ? item.name.substr(0, 18) + '...' : item.name;// 	label = label.length > 10 ? label.substr(0, 10) + '\n' + label.substr(10) : label;// 	return {// 		id: index,// 		longitude: Number(location[0]),// 		latitude: Number(location[1]),// 		iconPath,// 		joinCluster: this.zoom > 18 ? false : true,// 		width: 50,// 		height: 60,// 		name: item.name,// 		label: {// 			content: label,// 			anchorX: 20,// 			anchorY: -45,// 			color: '#333',// 			fontSize: 14// 		}// 	}// }).filter(i => i !== undefined)console.log('markers', this.markers)if (this.markers.length === 0) {uni.showToast({title: '暂无相关点位信息',icon: "none"})this.longitude = 92.066757this.latitude = 31.473608this.zoom = 16return}// })},// 点击标记点callouttap(e) {this.showMarkerListPop = true// 根据markerid查找console.log('点击标记点E', e) //e.markerId},regionchange() {// this.mapContext.getScale({// 	success: (res) => {// 		this.zoom = res.scale// 	}// })},update() {console.log('渲染更新完成') //e.markerId},// 定位到当前位置backCenter() {uni.getLocation({type: 'gcj02',success: (res) => {if (res.longitude && res.latitude) {this.latitude = Number(res.latitude)this.longitude = Number(res.longitude)this.zoom = 16.01 // 必须要有层级的变化,下面的方法才能生效this.mapContext.moveToLocation({longitude: this.longitude,latitude: this.latitude,success: (res) => {this.zoom = 16.03},fail: (info) => {console.log(info)}})} else {uni.showToast({title: '无法获取当前定位'})}},fail(err) {uni.showToast({title: '无法获取当前定位'})}});},}}
</script><style lang="scss" scoped>.marker-map {.layer {position: fixed;z-index: 999;width: 100%;right: 0;bottom: 0;}.cur-location {width: 86rpx;height: 86rpx;background: #FFFFFF;box-shadow: 0rpx 0rpx 8rpx 1rpx rgba(0, 144, 255, 0.15);border-radius: 20rpx 20rpx 20rpx 20rpx;margin-right: 30rpx;margin-bottom: 50rpx;}.list {width: 100%;height: 100%;}}
</style>

http://www.lryc.cn/news/465096.html

相关文章:

  • Spring Boot在线考试系统:JavaWeb技术的应用案例
  • Linux在服务器多节点下面如何快速查找日志
  • 模数转换ADC0804的应用
  • CBAM中关于碳关税抵销
  • 2.5 windows xp,ReactOS系统快速系统调用的实现
  • AI助力广交会,人工智能在制造业有哪些应用场景?
  • ElementPlus-Table表格-单选--TypeScript进阶篇
  • 三大智能体平台深度对比:字节Coze、百度AppBuilder、智谱智能体优劣解析
  • 【Flutter】基础入门:自定义Widget
  • 程序员日志之DNF手游装备武器升级55级攻略
  • Java项目-基于springcloud框架的分布式架构网上商城系统项目实战(附源码+文档)
  • ComfyUI_LayerStyle节点介绍
  • SQL Injection | SQL 注入 —— 时间盲注
  • 最新开发项目H5商城小程序源码系统 带源代码安装包以及搭建部署教程
  • 5大绝招揭秘:Cursor如何让RESTful API开发效率提升300%?
  • 鸿蒙开发,在 ArkTS 中,如何使用 Column 实现垂直居中对齐
  • 红日安全vulnstack (一)
  • 为什么SSH协议是安全的?
  • 主键 外键
  • G - Road Blocked 2
  • R语言绘制Venn图(文氏图、温氏图、维恩图、范氏图、韦恩图)
  • 【Vue.js】vue2 项目在 Vscode 中使用 Ctrl + 鼠标左键跳转 @ 别名导入的 js 文件和 .vue 文件
  • NVM配置与Vue3+Vite项目快速搭建指南
  • 面试“利器“——微学时光
  • 【Unity】【游戏开发】游戏引擎是如何模拟世界的
  • vscode配置conda虚拟环境【windows系统】
  • libgpiod在imx8平台交叉编译说明
  • 无人机之自主飞行关键技术篇
  • performance.timing
  • 教你不用下载 maven,不用配置环境变量,在 idea 上创建 maven 项目