2015-10-19 60 views
0

如何避免缩放this.map.fitBounds()? 做这个.map.fitBounds将放大或缩小映射以拟合边界值。我想避免fitBounds的缩小功能。我可以这样做吗?避免缩小this.map.fitBounds()

试过following.This将在执行fitBounds()后执行。需要在执行fitBounds之前执行()

var lineBounds = this._trail.getLineBounds() 
, current_zoom = this.map.getZoom() 
, center = this.map.getCenter(); 

this.map.fitBounds(lineBounds, { 
    padding: [29, 29], 
}); 

this.map.once('zoomend', function() { 
    if (current_zoom > this.map.getZoom()) { 
     this.map.setView(center, current_zoom); 
    } 
}.bind(this)); 

回答

1

你必须设置minZoom选项地图实例。问题是你需要手动编辑地图实例的选项对象。在Leaflet v1.0.0-beta.2中引入了setMinZoomsetMaxZoom方法,而Mapbox尚未使用该版本。否则,你可以简单地做map.setMinZoom(number),现在你不得不求助于map.options.minZoom = number。因此,在拟合边界后,使用map.getZoom方法获取地图的当前缩放因子,并将该因子值设置为minZoom选项。