uniapp 小程序 地图<map> 渲染标注点 且自定义气泡内容(slot写到页面中/样式自定义)

效果图: (气泡可随意定义样式)

页面内容:  (slot为自定义标注气泡)

<map :style="mapStyle" show-location="true" :latitude="latitude" id="map" :longitude="longitude":markers="markers">
	<cover-view slot="callout">
		<block v-for="(item,index) in markers" :key="index">
			<cover-view class="customCallout" :marker-id="item.id">
				<cover-view class="content">
					{
   
   {item.id}} 内容
				</cover-view>
			</cover-view>
		</block>
	</cover-view>
</map>

获取经纬度方法:

//获取经纬度
getLication: function() {
	var that = this;
	uni.getLocation({
		type: 'gcj02',
		success: function(res) {
			// console.log('当前位置的经度:' + res.longitude);
			// console.log('当前位置的纬度:' + res.latitude);
			that.longitude = res.longitude;
			that.latitude = res.latitude;
		},
		fail(err) {
			console.log(err);
		}
	});
},

数据整理方法: (整理各坐标点经纬度和信息到maekers中)

//获取到的充电站展示到地图中
filterMapMarkers: function() {
	var list = this.changingStationList;
	console.log("充电站list", list)
	var markers = [];
	for (let i = 0; i < list.length; i++) {
		let markersItem = {
			id: i,
			latitude: list[i].location[0],
			longitude: list[i].location[1],
			iconPath: '/static/image/icon/icon-map-location.png',
			width: '35rpx',
			height: '46rpx',
			customCallout: {
			anchorY: 0, // Y轴偏移量
			anchorX: 0, // X轴偏移量
			display: 'ALWAYS' ,// 一直展示
			
			},
		}
		markers.push(markersItem);
	}
	this.markers = markers;
},

css内容:

扫描二维码关注公众号,回复: 15007170 查看本文章
.customCallout {
	box-sizing: border-box;
	background-color: #fff;
	background: #FFFFFF;
	box-shadow: 0px 4rpx 16px 0px rgba(189, 191, 193, 0.4);
	border-radius: 4rpx;
	display: inline-flex;
	padding: 6rpx 24rpx;
	justify-content: center;
	align-items: center;
	color: #2A7BE2;
}

猜你喜欢

转载自blog.csdn.net/weixin_44805839/article/details/128207937