微信小程序map组件所有markers展示在视野范围内

注意:使用include-points属性不生效,要通过createMapContext实现

javascript 复制代码
<template>
	<view class="map-box">
		<map id="map" class="map" :markers="markers" :enable-traffic="true" :enable-poi="true"></map>
		<view class="bottom">
			<view @click="choose" style="text-align: center;">选择位置</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "ty-wx-map",
		data() {
			return {
				markers: [{
					id: 1,
					callout: {
						content: '北湖湿地',
						padding: 10,
						borderRadius: 2,
						display: 'ALWAYS'
					},
					latitude: 43.983281,
					longitude: 125.368689,
					iconPath: '../../static/Marker1_Activated@3x.png',
					width: '68rpx',
					height: '68rpx',
					rotate: 0,
					alpha: 1
				}, {
					id: 2,
					callout: {
						content: '南溪湿地',
						padding: 10,
						borderRadius: 2,
						display: 'ALWAYS'
					},
					latitude: 43.810332,
					longitude: 125.354891,
					iconPath: '../../static/Marker1_Activated@3x.png',
					width: '68rpx',
					height: '68rpx',
					rotate: 0,
					alpha: 1
				}, {
					id: 3,
					callout: {
						content: '西湖公园',
						padding: 10,
						borderRadius: 2,
						display: 'ALWAYS'
					},
					latitude: 43.873167,
					longitude: 125.162295,
					iconPath: '../../static/Marker1_Activated@3x.png',
					width: '68rpx',
					height: '68rpx',
					rotate: 0,
					alpha: 1
				}]
			};
		},
		mounted() {
			this.setMap()
		},
		methods: {
			choose() {
				const that = this
				uni.chooseLocation({
					success: function(res) {
						console.log('位置名称:' + res.name);
						console.log('详细地址:' + res.address);
						console.log('纬度:' + res.latitude);
						console.log('经度:' + res.longitude);
						that.markers.push({
							id: that.markers.length + 1,
							callout: {
								content: res.name,
								padding: 10,
								borderRadius: 2,
								display: 'ALWAYS'
							},
							latitude: res.latitude,
							longitude: res.longitude,
							iconPath: '../../static/Marker1_Activated@3x.png',
							width: '68rpx',
							height: '68rpx',
							rotate: 0,
							alpha: 1
						})
						that.setMap()
					}
				});
			},
			setMap() {
				const mapCtx = wx.createMapContext('map', this);
				mapCtx.includePoints({
					points: this.markers,
					padding: [36, 36, 10, 36]
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.map-box {
		position: absolute;
		width: 100%;
		z-index: 1;
		background-color: #fff;

		.map {
			width: 100%;
			height: 60vh;
			position: fixed;
		}

		.bottom {
			position: relative;
			z-index: 2;
			margin-top: calc(60vh - 24rpx);
			width: 100%;
			background-color: #fff;
			border-radius: 24rpx 24rpx 0 0;
			padding-bottom: env(safe-area-inset-bottom);
		}
	}
</style>

展示效果

点击选择位置,会把新选择的位置追加到地图markers并显示在视野范围内

相关推荐
MaCa .BaKa2 小时前
44-校园二手交易系统(小程序)
java·spring boot·mysql·小程序·maven·intellij-idea·mybatis
普通人62 小时前
微信小程序又双叒叕改获取头像昵称的接口了
微信小程序·小程序·notepad++
于先生吖11 小时前
高并发稳定运营,JAVA 动漫短剧小程序 + H5 源码
java·开发语言·小程序
2501_9159214311 小时前
uni-app一键生成iOS安装包并上传TestFlight全流程
android·ios·小程序·https·uni-app·iphone·webview
CHU72903511 小时前
旧衣新生之旅:旧衣服回收小程序的环保实践
小程序
闹小艾11 小时前
2026 知识付费线上课程小程序 SaaS制作平台深度评测:6 大维度拆解,教你选对不踩坑
大数据·小程序
一只小白菜11 小时前
Taro 4 + 支付宝小程序:Vite 编译报错 chunk.type undefined 的终极解决方案
小程序·taro
低代码布道师1 天前
微搭低代码MBA 培训管理系统实战 25——小程序用户登录与账号绑定
低代码·小程序
大黄说说1 天前
SaaS小程序制作平台对比:码云数智、有赞、微盟
微信小程序
清风絮柳2 天前
65.少儿英语微信小程序
vue.js·spring boot·微信小程序·小程序·毕业设计