Mapbox GL JS getBounds()/fitBounds()

前端 未结 5 1681
自闭症患者
自闭症患者 2020-12-13 17:44

I\'m using Mapbox GL JS v0.14.2 and I\'ve searched high and low through the documentation and very little is clear about this.

If you use the standard JS API, it\'s

5条回答
  •  被撕碎了的回忆
    2020-12-13 18:11

    For a solution that will work for all GeoJSON objects, not just a set of markers, check out Mapbox's Turf.js.

    This code was very helpful to me: https://bl.ocks.org/danswick/83a8ddff7fb9193176a975a02a896792

    But just to repeat the basics in case that link dies:

    var bounds = turf.bbox(markers);
    map.fitBounds(bounds, {padding: 20});
    

    The extent method mentioned in the linked code has been deprecated in favour of bbox, but the result is the same.

提交回复
热议问题