Toolkits@accelint/map-toolkit
toLonLat
Normalizes a picked deck.gl coordinate to a fresh [longitude, latitude] tuple, or null when it is empty or non-finite.
Usage
import { toLonLat } from '@accelint/map-toolkit/shared/coordinates';
import type { PickingInfo } from '@deck.gl/core';
// `info.coordinate` is `number[] | undefined`; normalize before storing it.
function onHover(info: PickingInfo) {
const coordinate = toLonLat(info.coordinate);
if (coordinate) {
setCoordinate(coordinate);
}
}Reference
function toLonLat(value?: number[] | null): [number, number] | nullParameters
| Parameter | Type | Description |
|---|---|---|
value | number[] | null | undefined | Picked coordinate to normalize, typically PickingInfo.coordinate. |
Returns
Returns a new [longitude, latitude] tuple when the first two entries are finite numbers, otherwise null. A trailing z entry is dropped.
deck.gl's PickingInfo.coordinate is typed number[] and can be empty, non-finite, or three-element ([longitude, latitude, z] when any layer is pickable: '3d'), so pass it through toLonLat before treating it as a coordinate.
Good to know:
toLonLatis a pure function and always returns a fresh tuple; it never returns the input array. It runs on every hover and drag insideBaseMap, so it performs two direct checks with no callback allocation.
Examples
Example: Guarding a bus payload
import { toLonLat } from '@accelint/map-toolkit/shared/coordinates';
const coordinate = toLonLat(info.coordinate);
// Skip the emit when the pointer is off the globe.
if (coordinate) {
emitDragStart({ id, coordinate, shiftKey, ctrlKey, altKey });
}Example: Dropping a z component
import { toLonLat } from '@accelint/map-toolkit/shared/coordinates';
toLonLat([-97.74, 30.27, 120]);
// [-97.74, 30.27]Example: Rejecting invalid input
import { toLonLat } from '@accelint/map-toolkit/shared/coordinates';
toLonLat(undefined); // null
toLonLat(null); // null
toLonLat([]); // null
toLonLat([Number.NaN, 30.27]); // null
toLonLat([-97.74]); // nullRelated
- BaseMap - Normalizes drag coordinates with
toLonLatbefore emittingmap:dragStart/map:drag/map:dragEnd - Measurement Layer - Applies
toLonLattopointA/pointBso non-finite points render nothing - cursor-coordinates - Track and format cursor coordinates in multiple systems