实现这个功能需要借助turf库:
const data = {
type: 'Feature',
geometry: geo,
}
const bound = bbox(geo)
// 方案一
const min = map.project([bound[0], bound[1]])
const max = map.project([bound[2], bound[3]])
map.fitScreenCoordinates(min, max, this.map.getBearing())
// 方案二
map.fitBounds(bound)
project方法:
官网的解释:Returns a Point representing pixel coordinates, relative to the map's container.
翻译过来就是:会返回一个经纬度点相对于地图容器(#map)的像素坐标
fitScreenCoordinates方法:
官网解释:Pans, rotates and zooms the map to to fit the box made by points p0 and p1 once the map is rotated to the specified bearing. To zoom without rotating, pass in the current map bearing.
翻译:让地图窗口到p0、p1坐标的范围。
fitBounds方法:
跟上面方法一样只不过传的是经纬度坐标,不是像素坐标。