问题:设置了三个坐标,绘制了线路,需要视野里显示完整路线。但是按照官网给的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,
});
}