fix(collections): the collection map opens on all of its places

The map framed its first view for the browser window instead of its own box and then skipped the fit, so a list of cafes across Europe opened on northern France. It now fits the places in its own box with room for the label bar and the controls, fits again when the set of places changes, keeps a view the user moved, opens a single place at town level and keeps a list across the date line on its short arc.
This commit is contained in:
Maurice
2026-10-25 03:12:18 +00:00
parent 44cefc2fe2
commit 3681db6cd4
13 changed files with 470 additions and 39 deletions
+24
View File
@@ -1,5 +1,6 @@
import { describe, it, expect } from 'vitest'
import {
alongShortestArc,
computeMapViewport,
MAX_ZOOM_GL,
MAX_ZOOM_RASTER,
@@ -263,3 +264,26 @@ describe('computeMapViewport', () => {
expect(withPadding.zoom).toBe(equivalent.zoom)
})
})
describe('alongShortestArc', () => {
const FIJI: [number, number] = [-18.14, 178.44]
const TONGA: [number, number] = [-21.14, -175.2]
const SAMOA: [number, number] = [-13.83, -171.76]
it('carries the places past the antimeridian on beyond 180, so a min-max box between them is the short one', () => {
const lngs = alongShortestArc([FIJI, TONGA, SAMOA]).map(([, lng]) => Math.round(lng * 100) / 100)
// From Fiji east across the date line: under ten degrees wide, not the 350 the other way round.
expect(lngs).toEqual([178.44, 184.8, 188.24])
})
it('leaves places that do not cross the antimeridian as they are', () => {
const cafes: [number, number][] = [[48.21, 16.37], [38.71, -9.14], [55.7, 12.55]]
expect(alongShortestArc(cafes)).toEqual(cafes)
})
it('has nothing to lay out for no places', () => {
expect(alongShortestArc([])).toEqual([])
})
})
+12
View File
@@ -146,6 +146,18 @@ function lngExtent(lngs: number[], wrapsWorld: boolean): { west: number; east: n
return { west, east, span: east - west }
}
/**
* `[lat, lng]` points with their longitudes laid along the narrowest arc that holds them all
* (lngExtent), for a renderer that wraps the world: a point past the antimeridian goes on
* beyond 180 instead of starting over at -180. A bounding box built from them by plain min and max (MapLibre's and Mapbox's
* extend) then spans the few degrees from Fiji to Samoa, not the rest of the world.
*/
export function alongShortestArc(points: readonly [number, number][]): [number, number][] {
if (points.length === 0) return []
const { west } = lngExtent(points.map(([, lng]) => lng), true)
return points.map(([lat, lng]) => [lat, lng < west ? lng + 360 : lng])
}
/** Zoom at which a span covering `fraction` of the world fills `px` pixels. */
function zoomForFraction(px: number, tileSize: number, fraction: number, maxZoom: number): number {
// A zero fraction (identical coordinates) would divide by zero and yield Infinity.