Returns the approximate distance between a pair of coordinates in meters Uses the Haversine Formula (from R.W. Sinnott, "Virtues of the Haversine", Sky and Telescope, vol. 68, no. 2, 1984, p. 159)
coordinates to compute the distance to
Distance in meters between the two coordinates.
let new_york = new LngLat(-74.0060, 40.7128);
let los_angeles = new LngLat(-118.2437, 34.0522);
new_york.distanceTo(los_angeles); // = 3935751.690893987, "true distance" using a non-spherical approximation is ~3966km
Static
convertConverts an array of two numbers or an object with lng
and lat
or lon
and lat
properties
to a LngLat
object.
If a LngLat
object is passed in, the function returns it unchanged.
An array of two numbers or object to convert, or a LngLat
object to return.
A new LngLat
object, if a conversion occurred, or the original LngLat
object.
let arr = [-73.9749, 40.7736];
let ll = LngLat.convert(arr);
ll; // = LngLat {lng: -73.9749, lat: 40.7736}
A
LngLat
object represents a given longitude and latitude coordinate, measured in degrees. These coordinates are based on the WGS84 (EPSG:4326) standard.MapLibre GL JS uses longitude, latitude coordinate order (as opposed to latitude, longitude) to match the GeoJSON specification.
Note that any MapLibre GL JS method that accepts a
LngLat
object as an argument or option can also accept anArray
of two numbers and will perform an implicit conversion. This flexible type is documented as LngLatLike.Example
See