Skip to content

Commit

Permalink
feat(staticMaps): Support style and accessToken as URL query params
Browse files Browse the repository at this point in the history
  • Loading branch information
gmaclennan authored and okdistribute committed Sep 14, 2020
1 parent 69dc324 commit 49507f2
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/background/maps.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const logger = require('../logger')
const CONCURRENCY = 4
const queue = new PQ({ concurency: CONCURRENCY })

const accessToken = config.MAPBOX_ACCESS_TOKEN
const style = 'mapbox://styles/mapbox/outdoors-v10'
const fallbackAccessToken = config.MAPBOX_ACCESS_TOKEN
const fallbackStyle = 'mapbox://styles/mapbox/outdoors-v10'

const getMapboxInstance = (() => {
let cur = 0
Expand All @@ -20,7 +20,7 @@ const getMapboxInstance = (() => {
if (!instances) {
instances = new Array(CONCURRENCY)
.fill(null)
.map(() => new HiddenMapbox({ accessToken, style }))
.map(() => new HiddenMapbox())
}
cur = (cur + 1) % CONCURRENCY
return instances[cur]
Expand All @@ -30,11 +30,15 @@ const getMapboxInstance = (() => {
var router = Router()

router.addRoute('/map/:lon/:lat/:zoom/:width/:height/x:pixelRatio.png', function (req, res, params) {
const { searchParams } = new URL(req.url, 'http://' + req.headers.host)
const { lon, lat, zoom, width, height, pixelRatio } = params

const promise = queue.add(() => {
const mapbox = getMapboxInstance()
// TODO: Catch map errors (e.g. invalid style) and return error
return mapbox.getMapImage({
style: searchParams.get('style') || fallbackStyle,
accessToken: searchParams.get('accessToken') || fallbackAccessToken,
center: {lon, lat},
zoom: parseInt(zoom),
width: parseInt(width),
Expand Down Expand Up @@ -65,7 +69,8 @@ router.addRoute('/map/:lon/:lat/:zoom/:width/:height/x:pixelRatio.png', function

module.exports = {
handle: (req, res) => {
var route = router.match(req.url)
const { pathname } = new URL(req.url, 'http://' + req.headers.host)
var route = router.match(pathname)
if (!route) return false
route.fn.apply(null, [req, res, route.params, route.splats])
return true
Expand Down

0 comments on commit 49507f2

Please sign in to comment.