digitalfabrik/integreat-app

View on GitHub
web/src/utils/moveViewportToCity.ts

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
import WebMercatorViewport from '@math.gl/web-mercator'

import { defaultMercatorViewportConfig, MapViewMercatorViewport } from 'shared'
import { CityModel } from 'shared/api'

const moveViewportToCity = (city: CityModel, zoom: number | undefined): MapViewMercatorViewport => {
  const mercatorViewport = new WebMercatorViewport(defaultMercatorViewportConfig)
  const boundingBox = city.boundingBox
  const viewport = mercatorViewport.fitBounds([
    [boundingBox[0], boundingBox[1]],
    [boundingBox[2], boundingBox[3]],
  ])
  return { ...viewport, zoom: zoom ?? viewport.zoom }
}

export default moveViewportToCity