I need restrict map area to 5km radius around given point. How can I get appropriate bounding box using mapbox-gl-js or turf?
Asked
Active
Viewed 3,285 times
2 Answers
9
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>

Andi-lo
- 2,244
- 21
- 26
-
1you can use turf.point for initial. `var point = turf.point([-90.548630, 14.616599])` – YairTawil Oct 28 '18 at 08:39
4
Buffer API has changed. Buffer's optional argument needs to be an object like so:
var point = turf.point([-90.548630, 14.616599]);
var buffered = turf.buffer(point, 5, {units:'kilometers'}); /// notice 'units'
var bbox = turf.bbox(buffered);
console.log(turf.bboxPolygon(bbox));

mbejda
- 1,471
- 16
- 25
-
Please comment the post if the API has changed and don't duplicate my answer with a redundant one – Andi-lo Mar 25 '19 at 16:17
-
4Your approach was outdated and misleading. I'm happy you copied over the fix. – mbejda Apr 02 '19 at 19:39