微信小程序 map 自动缩放显示所有给定经坐标点 设置include-points不生效

文章讲述了在微信小程序中使用地图组件时,遇到include-points属性无法显示完整路线的问题,提供了两种解决方案:主动触发includePoints方法和异步设置。作者强调了在微信开发者工具中可能需要额外处理的情况。

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

问题:设置了三个坐标,绘制了线路,需要视野里显示完整路线。但是按照官网给的include-points属性设置,发现无效果。
摘自微信官方文档:
在这里插入图片描述
wxml文件:

<map id="map" class="receive_map" include-points='{{points}}' polyline="{{polyline}}" markers="{{markers}}" ></map>

解决办法一:创建 map 对象,用includePoints方法来主动触发。

onLoad: function () {
        let mapCtx = wx.createMapContext('map');
        const points = [
              {
		        latitude: 35.42,
		        longitude: 119.53
		      }, {
		        latitude: 31.24,
		        longitude: 121.50
		      }, {
		        latitude: 30.74,
		        longitude: 120.74,
		      }
        ]
        var polyline = [{
	      points: points,
	      width: 2
	    }];

        mapCtx.includePoints({
            padding: [20, 20, 20, 20],
            points: points,
        });
    }

如果微信开发者工具还不生效,异步设置includePoints

	onLoad: function () {
        setTimeout(()=>{
	      this.setMap()
	    }, 2000)
    }
    setMap() {
    let mapCtx = wx.createMapContext('map');
        const points = [
              {
		        latitude: 35.42,
		        longitude: 119.53
		      }, {
		        latitude: 31.24,
		        longitude: 121.50
		      }, {
		        latitude: 30.74,
		        longitude: 120.74,
		      }
        ]
        var polyline = [{
	      points: points,
	      width: 2
	    }];

        mapCtx.includePoints({
            padding: [20, 20, 20, 20],
            points: points,
        });
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值