Standard Toolkit
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] | null

Parameters

ParameterTypeDescription
valuenumber[] | null | undefinedPicked 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: toLonLat is a pure function and always returns a fresh tuple; it never returns the input array. It runs on every hover and drag inside BaseMap, 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]); // null
  • BaseMap - Normalizes drag coordinates with toLonLat before emitting map:dragStart / map:drag / map:dragEnd
  • Measurement Layer - Applies toLonLat to pointA / pointB so non-finite points render nothing
  • cursor-coordinates - Track and format cursor coordinates in multiple systems

On this page