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

【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)
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
1. 使用缩放视角 可以通过设置地图缩放级别的方式,限制地图可移动的范围。在小程序中,可以使用 map 组件的 `scale` 属性设置地图的缩放级别。通过设置最大和最小的缩放级别,可以限制地图可移动的范围。 例如: ```html <map id="map" scale="{{scale}}" bindregionchange="regionchange" longitude="{{longitude}}" latitude="{{latitude}}" markers="{{markers}}" style="width: 100%; height: 300px;"></map> ``` ```javascript Page({ data: { scale: 14, // 地图缩放级别 longitude: 114.06667, // 中心经度 latitude: 22.61667, // 中心纬度 markers: [] }, regionchange(e) { // 监听地图缩放事件 if (e.type === 'end') { const { scale, longitude, latitude } = this.data; if (scale < 14) { // 缩放级别小于14时,重置地图中心点 this.setData({ longitude: 114.06667, latitude: 22.61667 }); } if (longitude < 113.5 || longitude > 114.5 || latitude < 22 || latitude > 23) { // 经纬度超出范围时,重置地图中心点 this.setData({ longitude: 114.06667, latitude: 22.61667 }); } } } }); ``` 在上面的代码中,当地图缩放级别小于14时,将地图中心点重置为指定的经纬度。当地图中心点超出指定的经纬度范围时,也将地图中心点重置为指定的经纬度。 2. 使用地图限制区域 在高德地图和腾讯地图中,可以使用 `setLimitBounds` 方法设置地图的限制区域。在小程序中,可以通过调用 map 组件的 `getMapContext` 方法获取地图上下文,然后调用 `setLimitRegion` 方法设置地图的限制区域。 例如: ```javascript Page({ onReady() { this.mapCtx = wx.createMapContext('map'); this.mapCtx.setLimitRegion({ // 设置地图限制区域 southwest: { latitude: 22, longitude: 113.5 }, northeast: { latitude: 23, longitude: 114.5 } }); } }); ``` 在上面的代码中,设置地图的限制区域为经度在 113.5 - 114.5 之间,纬度在 22 - 23 之间的区域。当用户拖动地图时,地图将受到限制,无法超出指定的区域。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值