微信小程序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并显示在视野范围内

相关推荐
weixin_1772972206939 分钟前
剧本杀小程序开发:如何通过数据驱动提升用户体验?
小程序·ux·剧本杀
小着1 小时前
微信小程序组件中二维码生成问题解决方案
前端·微信小程序
sen_shan1 小时前
《微信小程序》第六章:参数定义与管理
微信小程序·小程序
他们叫我秃子5 小时前
从 0 到 1,我用小程序 + 云开发打造了一个“记忆瓶子”,记录那些重要的日子!
前端·微信小程序·小程序·云开发
2501_915106325 小时前
iOS 反编译防护工具与实战组合 从静态侦察到 IPA 成品加固的工程化路径
android·ios·小程序·https·uni-app·iphone·webview
星光一影6 小时前
快递比价寄件系统技术解析:基于PHP+Vue+小程序的高效聚合配送解决方案
vue.js·mysql·小程序·php
苹果电脑的鑫鑫7 小时前
微信小程序原生如何使用画布生成名片
微信小程序·小程序
Dragon Wu7 小时前
Taro 自定义tab栏和自定义导航栏
前端·javascript·小程序·typescript·前端框架·taro
游戏开发爱好者89 小时前
iOS 26 iPhone 使用记录分析 多工具组合构建全方位设备行为洞察体系
android·ios·小程序·uni-app·cocoa·iphone·webview
星光一影1 天前
Java医院管理系统HIS源码带小程序和安装教程
java·开发语言·小程序