Skip to content

Instantly share code, notes, and snippets.

@rgazelot
Created March 19, 2020 09:42
Show Gist options
  • Save rgazelot/4f3b1888984260c6b89c878daea4b328 to your computer and use it in GitHub Desktop.
Save rgazelot/4f3b1888984260c6b89c878daea4b328 to your computer and use it in GitHub Desktop.
Final version
const bounds = poster.bounds
// Use geoViewport to determine the center and the zoom level of the entire map.
const viewport = geoViewport.viewport([
bounds['_sw'].lng,
bounds['_sw'].lat,
bounds['_ne'].lng,
bounds['_ne'].lat
], [size.width, size.height], undefined, undefined, 512, true)
const zoom = viewport.zoom
const merc = new SphericalMercator({ size: 512 })
// Determine the x and y of each tiles of the matrix using the
// SphericalMercator library
const xyz = merc.xyz([
bounds['_sw'].lng,
bounds['_sw'].lat,
bounds['_ne'].lng,
bounds['_ne'].lat,
], zoom)
const coord = [xyz.minX, xyz.minY]
const nbXTiles = xyz.maxX - xyz.minX + 1
const nbYTiles = xyz.maxY - xyz.minY + 1
const images = []
for (let i=0; i<(nbXTiles*nbYTiles); i++) {
// For each tile, find the bbox according to the x and y coords.
const bbox = merc.bbox(coord[0], coord[1], zoom)
// Find the center en the zoom level according to the bbox
const imageViewport = geoViewport.viewport([
bbox[0],
bbox[1],
bbox[2],
bbox[3]
], [512, 512], undefined, undefined, 512, true)
images.push(imageViewport)
coord[1] = coord[0] === xyz.maxX ? coord[1] + 1 : coord[1]
coord[0] = coord[0] === xyz.maxX ? xyz.minX : coord[0] + 1
}
// I'm now able to build the call to the Static Image API using more accurate centers and zoom levels.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment