微信小程序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/[email protected]',
					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/[email protected]',
					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/[email protected]',
					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/[email protected]',
							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并显示在视野范围内

相关推荐
小咕聊编程13 分钟前
【含文档+PPT+源码】基于微信小程序的社区便民防诈宣传系统设计与实现
微信小程序·小程序
❀͜͡傀儡师34 分钟前
使用mermaid 语言绘画时序图和链路图
notepad++
向明天乄2 小时前
uni-app微信小程序登录流程详解
微信小程序·uni-app
小新1102 小时前
微信小程序学习之轮播图swiper
学习·微信小程序·notepad++
前端 贾公子2 小时前
UniApp 微信小程序绑定动态样式 :style 避坑指南
notepad++
熊猫钓鱼>_>5 小时前
中国版Cursor,基于腾讯云CodeBuddy的节日推荐小程序的智能化开发实践
小程序·腾讯云·节日
AC-PEACE7 小时前
小程序初始化加载时间优化 步骤思考与总结
react.js·小程序·前端框架
老李不敲代码7 小时前
榕壹云打车系统:基于Spring Boot+MySQL+UniApp的开源网约车解决方案
spring boot·mysql·微信小程序·uni-app·软件需求
说私域7 小时前
桑德拉精神与开源链动2+1模式AI智能名片S2B2C商城小程序的协同价值研究
人工智能·小程序·开源·零售
说私域9 小时前
基于开源AI大模型与S2B2C生态的个人品牌优势挖掘与标签重构研究
人工智能·小程序·开源·零售