【uniapp 小程序】解决 map 组件出现标点(地图)自动偏移或 @regionchange 频繁触发的问题

文章讲述了在uniapp小程序中遇到的地图组件问题,包括标点自动偏移和regionchange频繁触发。作者提供了代码示例,展示了如何通过限制regionchange事件和节流处理来解决这些问题。

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

【uniapp / 小程序】解决 map 组件出现标点(地图)自动偏移或 @regionchange 频繁触发的问题

在业务开发中出现了地图的中心标点向右侧缓慢移动的问题,在我解决后又发现了 @regionchange 方法出现了无限调用的问题。这几个问题属于微信 map 地图组件迟迟未修复的bug。

本文仅解决与我相似的问题以做参考,并会附上对应的问题与参考的博客。

一、问题复现

1、地图无操作下出现缓慢的自行移动:

original-original

2、无触发下提示信息被一直触发:

original-original-1

3、相关的问题代码

<template>
	<view>
		<map id="map" :scale="scale[scaleIndex].name" :circles="circles" :latitude="lat" :longitude="lng"
				:markers="markers" :show-location="true" @regiοnchange="changeRegion" @callouttap="handleCallout"
				@tap="tapMap">
			</map>
	</view>
</template>
  • scale:缩放
  • circles:区域圆
  • latitude\longitude:地图中心坐标
  • markers:标点
  • show-location:带箭头的当前坐标
  • regionchange:视角移动切换时触发
  • callouttap:点击标点事件
  • tap:点击地图触发事件
changeRegion(e) { //中心点标记始终在地图中心位置不动
	let that = this;
	this.mapCtx = uni.createMapContext('map');
	this.mapCtx.getCenterLocation({
		success(res) {
			const latitude = res.latitude
			const longitude = res.longitude
             console.log(latitude, longitude);
			console.log(that.lat, that.lng);
			that.lat = latitude
			that.lng = longitude
			if (that.scrollTimer) {
				clearTimeout(that.scrollTimer);
			}
			that.scrollTimer = setTimeout(() => {
				that.latitude = res.latitude;
				that.longitude = res.longitude;
				that.getMapHouses(res.longitude, res.latitude); // 请求区域内存在数据
			}, 1500)
		}
	})
},

二、问题解决

主要问题存在于 @regionchange 方法不断被触发,且不移动时获取的维度坐标总比我们当前的 -1 。

image-20231116151207139

解决代码:

通过限制静态时触发的偏移量,不触发对应的区域接口,各位的数据偏移量和方向可能各有不同,需要根据具体的问题进行设置。

changeRegion(e) { //中心点标记始终在地图中心位置不动
	console.log(e);
	let that = this;
	if (e.type == 'end') { // 仅获取结束坐标
		this.mapCtx = uni.createMapContext('map');
		this.mapCtx.getCenterLocation({
			success(res) {
				const latitude = res.latitude
				const longitude = res.longitude

				if ((that.lng - longitude) < 0.000005 && (that.lng - longitude) > 0 &&
					latitude == that.lat) { // 对静态移动标点做限制防止偏移
					return
				}
				if (that.scrollTimer) { // 设置节流,进一步限制高频触发
					clearTimeout(that.scrollTimer);
				}
				that.scrollTimer = setTimeout(() => {
					that.lat = latitude
					that.lng = longitude
					that.latitude = res.latitude;
					that.longitude = res.longitude;
					that.getMapHouses(res.longitude, res.latitude); // 请求区域内存在数据
				}, 1500)
			}
		})
	} else { // begin
	}
},

参考文献:

  1. 微信小程序–地图regionchange事件频繁触发导致崩溃-CSDN博客
  2. 微信小程序 地图定位、选址,解决regionchange重复调用-CSDN博客
  3. 微信小程序 地图定位、选址,解决regionchange重复调用-CSDN博客
  4. Map 组件在开发者工具中一直自己移动,真机正常 | 微信开放社区 (qq.com)
### UniApp 微信小程序 Map 组件实现缩放及车辆位置聚合查询 #### 地图组件基础设置 为了在 UniApp 中使用微信小程序地图功能,需先引入并配置 `map` 组件。此组件支持多种交互操作,如缩放、拖拽等[^2]。 ```html <template> <view class="container"> <!-- 地图容器 --> <map id="myMap" :latitude="centerLat" :longitude="centerLng" scale="14" show-scale bindregionchange="bindRegionChange"></map> </view> </template> <script> export default { data() { return { centerLat: 39.9087, // 默认中心纬度 centerLng: 116.3975 // 默认中心经度 }; }, methods: { bindRegionChange(e) { // 处理地图区域变化事件 console.log('当前比例尺:', e.detail.scale); } } }; </script> ``` #### 实现缩放控制 通过监听 `regionchange` 事件可以实时获取用户的缩放行为,并据此调整显示逻辑加载更多数据[^4]。 #### 车辆位置聚合处理 对于大量标记点(例如车辆),直接渲染可能导致性能下降。此时应采用 Marker Cluster 技术对相近的标记进行聚类展示: ```javascript // 初始化地图上下文对象 const mapCtx = uni.createMapContext('myMap'); function initVehicleMarkers(vehiclePositions) { const markers = vehiclePositions.map((pos, index) => ({ id: index, latitude: pos.lat, longitude: pos.lng, iconPath: '/static/car.png', width: 20, height: 20 })); // 添加所有车辆标记到地图mapCtx.addMarkers({ markers: markers, success(res) { console.log('成功添加了 ' + res.addedCount + ' 辆车'); // 对这些标记启用聚合效果 (如果需要的话) mapCtx.initMarkerCluster(); } }); } ``` #### 动态更新与查询 随着用户不断放大缩小视窗范围,可能需要动态刷新可见区域内车辆的位置信息。这通常涉及到服务器端的数据筛选和服务接口调用[^3]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值