mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
The vessel source accepts an optional area and sends it as lat, lon and radius_km. The vessel layer asks for the area the camera shows (centered on the middle of the screen, sized by camera height), or every vessel from high enough up, and asks again when the view moves or zooms by a quarter of that radius. vessels_in_area names its area too. Servers that hold every vessel may ignore the area; callers still keep only what is inside it.
34 lines
1.2 KiB
JavaScript
34 lines
1.2 KiB
JavaScript
import * as Cesium from 'cesium';
|
|
import { viewAreaRadiusKm } from './viewArea.js';
|
|
|
|
/**
|
|
* The area a viewer is looking at, as `{ lat, lon, radiusKm }`: centered on
|
|
* the point in the middle of the screen (or below the camera when the middle
|
|
* shows sky), sized by the camera's height. Null for a view that wants every
|
|
* vessel.
|
|
*/
|
|
export function vesselViewArea(viewer) {
|
|
const camera = viewer?.camera;
|
|
const scene = viewer?.scene;
|
|
const height = camera?.positionCartographic?.height;
|
|
const radiusKm = viewAreaRadiusKm(height);
|
|
if (radiusKm == null) return null;
|
|
let center = camera.positionCartographic;
|
|
const canvas = scene?.canvas;
|
|
if (canvas?.clientWidth && canvas?.clientHeight) {
|
|
try {
|
|
const hit = camera.pickEllipsoid(
|
|
new Cesium.Cartesian2(canvas.clientWidth / 2, canvas.clientHeight / 2),
|
|
scene.globe?.ellipsoid,
|
|
);
|
|
if (hit) center = Cesium.Cartographic.fromCartesian(hit);
|
|
} catch {
|
|
/* the camera position stands in */
|
|
}
|
|
}
|
|
const lat = Cesium.Math.toDegrees(center.latitude);
|
|
const lon = Cesium.Math.toDegrees(center.longitude);
|
|
if (!Number.isFinite(lat) || !Number.isFinite(lon)) return null;
|
|
return { lat, lon, radiusKm };
|
|
}
|