Skip to main content

Globe bounds fitting

Browsers, Workers, desktop scripts and script-enabled native libraries share cameraForBounds and fitBounds. Names and options follow the MapLibre API; SqsMap independently implements the spherical calculations.

// @gis-api 0.5-session
// @permissions ["project.read", "map.write"]
import { map } from 'sqsmap';
map.setProjection({ type: 'globe' });
await map.sqs.flush();
const bounds = [[170, -10], [-170, 30]];
const target = await map.cameraForBounds(bounds, {
  padding: 24, bearing: 20, maxZoom: 12
});
console.log(target); // Pure calculation; the camera stays unchanged.
map.fitBounds(bounds, { padding: 24, bearing: 20, duration: 600 });
await map.sqs.flush(); // Applied; observe moveend for completion.

Bounds also accept [west, south, east, north]. An east coordinate below west crosses the date line eastward. Current pitch is retained; bearing defaults to0 and min/max zoom to0/24. Padding accepts a nonnegative number or four-edge object; offset accepts logical pixel arrays or {x,y}. Padding constrains this target only and does not change getPadding(). Globe roll is connected; persistent globe padding is connected.

The implementation uses spherical visibility, bounded curved-edge/interior sampling and physical screen-anchor solving. Longitude spans of180 degrees or more, whole-world latitude spans, or bounds excluded by zoom limits/offsets fail explicitly and preserve the camera and active animation. Finite sampling does not promise analytical extrema for every possible extent.

fitBounds uses the shared timeline with duration, animate, linear, easing, essential, stop and reduced motion. Raw browser SDK methods are identical, with view.flush(). Native libraries send the same method/arguments through sqsm_map_camera_json; native hosts must render or pump scripts and drain events to advance animation. ABI and public method signatures are unchanged.

Current verification covers native Linux QuickJS, browser main thread and strict Worker. Windows/macOS, physical phones and hardware verification are recorded separately.