Search code examples
javascriptmapboxmapbox-gl-jsturfjs

How to get bounding box based on distance from given point?


I need restrict map area to 5km radius around given point. How can I get appropriate bounding box using mapbox-gl-js or turf?


Solution

  • You can use http://turfjs.org/docs/#buffer to get a feature in size of the radius that you want and then use http://turfjs.org/docs/#bbox to get the bbox.

    var point = turf.point([-90.548630, 14.616599]);
    var buffered = turf.buffer(point, 5, {units:'kilometers'});
    var bbox = turf.bbox(buffered);
    console.log(turf.bboxPolygon(bbox));
    <script src="https://npmcdn.com/@turf/turf/turf.min.js"></script>