Coordinate
Coordinate represents a coordinate point, e.g. a geographic point (longitude, latitude) or a projected coordinate. It is the base data structure for maptalks coordinate conversions, extending Position (the common parent of Point and Coordinate).
js
import { Coordinate } from "maptalks";
const coord = new Coordinate(0, 0);
const coord2 = new Coordinate([121.47, 31.23]);
const coord3 = new Coordinate({ x: 0, y: 0 });Constructor
js
new Coordinate(x, y, z?)
new Coordinate([x, y, z])
new Coordinate({ x, y, z })
new Coordinate(coordinate)Parameters:
- x
Numberx coordinate value. - y
Numbery coordinate value. - z
Numberz value (optional; a plain property not used in operations).
Array [x, y], JSON object {x, y}, and another Coordinate are also accepted.
Throws if x/y are
NaN.
Member Methods
Value & conversion
toArray(): number[]— convert to an array[x, y]([x, y, z]if z exists)toJSON(): { x, y, z? }— convert to a JSON objecttoFixed(n): Coordinate— round to n decimalscopy(): Coordinate— return a copy
Arithmetic
add(x, y?, z?): Coordinate— add another coordinatesub(x, y?, z?): Coordinate— subtract another coordinatesubstract(x, y?): Coordinate— alias ofsubmulti(ratio): Coordinate— multiply by a numberdiv(n): Coordinate— divide by a numberabs(): Coordinate— absolute valueround(): Coordinate— roundceil(): Coordinate— ceilfloor(): Coordinate— floor
Distance & comparison
distanceTo(point): number— Euclidean distance to a pointmag(): number— distance from the origincloseTo(p, delta?): boolean— whether within a deltaequals(c): boolean— whether equalisZero(): boolean— whether zero
In-place set
set(x, y, z?): this— set the coordinates (mutates and returns this)
Static Methods
Coordinate.toNumberArrays(coordinates)— convert Coordinates to GeoJSON-style coordinate arrays (recursive)Coordinate.toCoordinates(coordinates)— convert GeoJSON-style coordinates to Coordinates (recursive)
js
const coord = new Coordinate(121.47, 31.23);
const arr = coord.toArray(); // [121.47, 31.23]
const dist = coord.distanceTo(new Coordinate(121.48, 31.24));Coordinate is a pure data type with no event system. Coordinate projection conversion (lon/lat ↔ Web Mercator / pixel) is done by
CRS/projection classes, not by Coordinate methods.