uniapp使用地图map 点击marker出现弹窗

文章描述了一款使用Vue.js开发的前端应用,展示了如何在地图上添加标记并实现点击标记时弹出详细设备状态信息,涉及地理位置获取、API调用以及组件化开发过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<template>
	<view class="container page">
		<view class="map">
			<map id="map" style="width: 100%;flex: 1;" :longitude="longitude" :latitude="latitude" :markers="markers"
				scale="12" @markertap="markertap">
			</map>
		</view>
		<popup @close="onClose" :positionObj="positionObj" :title="title" v-if="isPopupVisible"></popup>
	</view>

</template>

<script>
	import api from '@/utils/api.js'
	import popup from '@/components/popup/popup.vue'
	export default {
		data() {
			return {
				markers: [],
				locationList: [],
				longitude: 119.49067788562778,
				latitude: 31.44029433767045,
				isPopupVisible: false,
				popupTitle: '弹窗标题',
				popupContent: '弹窗内容',
				positionObj: {},
				title: ""
			}
		},
		components: {
			popup
		},
		onLoad() {
			this.getLocation()
		},
		methods: {
			markertap(e) {
				// console.log('marker', e);
				this.title = this.locationList.find(it => it.gatewayId == e.detail.markerId).address
				api.post('/data/getRealData/?cpId=' + e.detail.markerId).then(res => {
					// console.log(res);
					this.positionObj = res.data[0]
				})
				this.isPopupVisible = true;

			},
			getLocation() {
				// api.get('')
				api.get('/cp/getCPList').then(res => {
					console.log(res)
					this.locationList = res.data
					this.markers = res.data.map((obj) => ({
						id: +obj.gatewayId,
						title: obj.address,
						longitude: +obj.longitude,
						latitude: +obj.latitude,
						iconPath: '/static/marker.png',
						width: 25,
						label: {
							content: obj.cpName,
							fontSize: 14,
							borderColor: '#234294',
							padding: 3,
							borderRadius: 4,
							anchorX: -35,
							borderWidth: 2
						},
					}))
				})
			},
			onConfirm() {
				// 点击确定按钮后的逻辑
				this.isPopupVisible = false;
			},
			onCancel() {
				// 点击取消按钮后的逻辑
				this.isPopupVisible = false;
			},
			onClose() {
				// 点击关闭按钮后的逻辑
				this.isPopupVisible = false;
			}
		}
	}
</script>

<style>
	.page {
		height: 100%;
	}

	.container {
		font-size: 14px;
		width: 750rpx;
		flex: 1;
	}

	.map {
		font-size: 14px;
		width: 750rpx;
		flex: 1;
	}
</style>
<template>
	<view class="popup">
		<p class="popup-title">监测点: {{title}}</p>
		<text class="popup-content">设备状态: 正常</text>
		<uni-grid :column="3" :showBorder="false" :square="false">
			<uni-grid-item>
				<text class="text" :style="filterFont(positionObj.phIsAlarm)">ph: {{positionObj.ph}}</text>
			</uni-grid-item>
			<uni-grid-item>
				<text class="text" :style="filterFont(positionObj.wdIsAlarm)">温度: {{positionObj.wd}}℃</text>
			</uni-grid-item>
			<uni-grid-item>
				<text class="text" :style="filterFont(positionObj.zdIsAlarm)">浊度: {{positionObj.zd}}NTU</text>
			</uni-grid-item>
			<uni-grid-item>
				<text class="text" :style="filterFont(positionObj.ylIsAlarm)">余氯: {{positionObj.yl}}mg/L</text>
			</uni-grid-item>
			<uni-grid-item>
				<text class="text" :style="filterFont(positionObj.ecIsAlarm)">电导率: {{positionObj.ec == null ?  '-' : positionObj.ec}} μs/cm</text>
			</uni-grid-item>
			<uni-grid-item>
				<text class="text">溶解氧: - mg/L</text>
			</uni-grid-item>
		</uni-grid>
		<!-- <button class="popup-close" @click="onClose">关闭</button> -->
		<view class="close">
			<uni-icons type="closeempty" size="30" @click="onClose"></uni-icons>
		</view>


	</view>
</template>

<script>
	export default {
		props: {
			positionObj: {
				type: Object,
				default: () => {}
			},
			title: {
				type: String,
				default: "中关村创智园"
			}
		},
		methods: {
			onConfirm() {
				// 点击确定按钮的逻辑
				this.$emit('confirm');
			},
			onCancel() {
				// 点击取消按钮的逻辑
				this.$emit('cancel');
			},
			onClose() {
				// 关闭弹窗的逻辑
				this.$emit('close');
			},
			filterFont(status) {
				switch (status) {
					case 0:
						// 正常
						return "color:#00b046";
						break;
					case 1:
						// 轻微
						return "color:#FFDD00";
						break;
					case 2:
						// 一般
						return "color:#FA7C06";
						break;
					case 3:
						// 严重
						return "color:#FF0000";
						break;
					default:
						break;
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.popup {
		/* 弹窗样式 */
		position: relative;
		box-shadow: 0px 4px 10px 0px rgba(0, 0, 0, 0.302);
		margin: 0 16rpx 10rpx;
		border-radius: 10rpx;
		padding: 20rpx 16rpx 30rpx;

		.close {
			position: absolute;
			top: 5%;
			right: 7%;
		}

		.popup-title {
			/* 弹窗标题样式 */
			font-family: 思源黑体;
			font-size: 28rpx;
			font-weight: 700;
			letter-spacing: 0em;
		}

		.popup-content {
			display: flex;
			/* 弹窗内容样式 */
			font-family: 思源黑体;
			font-size: 24rpx;
			font-weight: bold;
			letter-spacing: 0em;

			/* 设备状态: */
			color: #5C5C5C;

			.status {
				color: #1CCE05;
				font-size: 24rpx;
			}
		}

		.text {
			font-size: 28rpx;
		}

		.popup-buttons {
			/* 弹窗按钮样式 */
		}

		.popup-close {
			/* 弹窗关闭按钮样式 */
		}
	}
</style>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值