diff --git a/src/fit-bounds.js b/src/fit-bounds.js new file mode 100644 index 00000000..70d62b04 --- /dev/null +++ b/src/fit-bounds.js @@ -0,0 +1,27 @@ +// NOTE: Transform is not a public API so we should be careful to always lock +// down mapbox-gl to a specific major, minor, and patch version. +import Transform from 'mapbox-gl/js/geo/transform'; +import {LngLatBounds, Point} from 'mapbox-gl'; + +export default function fitBounds(width, height, _bounds, options) { + const bounds = new LngLatBounds([_bounds[0].reverse(), _bounds[1].reverse()]); + options = options || {}; + const padding = typeof options.padding === 'undefined' ? 0 : options.padding; + const offset = Point.convert([0, 0]); + const tr = new Transform(); + tr.width = width; + tr.height = height; + const nw = tr.project(bounds.getNorthWest()); + const se = tr.project(bounds.getSouthEast()); + const size = se.sub(nw); + const scaleX = (tr.width - padding * 2 - Math.abs(offset.x) * 2) / size.x; + const scaleY = (tr.height - padding * 2 - Math.abs(offset.y) * 2) / size.y; + + const center = tr.unproject(nw.add(se).div(2)); + const zoom = tr.scaleZoom(tr.scale * Math.min(scaleX, scaleY)); + return { + latitude: center.lat, + longitude: center.lng, + zoom + }; +} diff --git a/src/index.js b/src/index.js index 7e79ae32..576fe1bb 100644 --- a/src/index.js +++ b/src/index.js @@ -20,6 +20,8 @@ import MapGL from './map.react'; import * as overlays from './overlays'; +import fitBounds from './fit-bounds'; module.exports = MapGL; +module.exports.fitBounds = fitBounds; Object.assign(module.exports, overlays);