geo-viewport
Turns bounding boxes / extents into centerpoint & zoom combos for static maps.
Works in node.js and browsers, via browserify or a script tag.
Install
npm install --save geo-viewport
Or use a plugin:
The script-tag include exports an object called geoViewport
,
with methods bounds
and viewport
documented below.
Example
Live example with Mapbox Static Map API
With Node
var geoViewport = ; geoViewport // yields// {// center: [// 5.7604079999999955,// 45.189756500000016// ],// zoom: 11// }
In a browser:
api
viewport(bounds, dimensions, minzoom, maxzoom, tileSize)
Given a WSEN
array of bounds and a [x, y]
array of pixel
dimensions, return a { center: [lon, lat], zoom: zoom }
viewport.
Example:
// first argument is the bounds, and the image is 640x480geoViewport
bounds(viewport, zoom, dimensions, tileSize)
Given a centerpoint as [lon, lat]
or { lon, lat }
, a zoom,
and dimensions as [x, y]
, return a bounding box.
Example:
geoViewport
tile sizes
Be aware that these calculations are sensitive to tile size. The default size assumed by this library is 256x256px; however, Mapbox Vector Tiles are 512x512px.
For example, to calculating a bounding box for a classic raster-based 256x256 tile:
geoViewport // since 256 is default, you can omit the tileSize paramgeoViewport
To calculate a bounding box for a Mapbox vector tile source, such as an image from the Mapbox Static Image API:
geoViewport
There's a handy blog post discussing the issue here.