coordinateLast updated on
Last updated on
Build and validate geographic coordinates
Build and validate geographic coordinates. The Coordinate primitive represents a point on Earth using latitude and longitude values.
Examples
Creating coordinates
// Object pattern
const location = open.coordinate({ lat: 40.7128, lon: -74.0060 })
// Builder pattern
const location = open.coordinate()
.lat(40.7128)
.lon(-74.0060)
.build()
// Using point shorthand
const location = open.coordinate()
.point(40.7128, -74.0060)
.build()
// Copy and modify
const nearby = open.coordinate()
.from(location)
.lat(40.7130)
.build()Parsing external data
// Parse unknown input (throws on error)
const location = open.coordinate.parse(jsonData)
// Safe parsing (returns result object)
const result = open.coordinate.safeParse(jsonData)
if (result.success) {
console.log(`Location: ${result.data.lat}, ${result.data.lon}`)
} else {
console.error(result.error.message)
}Using with forms
const form = open.form({
name: 'location',
fields: {
meetingPoint: { type: 'coordinate', label: 'Meeting Point' }
}
})
const filled = form.fill({
fields: {
meetingPoint: open.coordinate().point(51.5074, -0.1278).build()
}
})API
Object Pattern
open.coordinate(input: Coordinate): CoordinatePass a Coordinate object for validation. Returns the validated Coordinate or throws on error.
const location = open.coordinate({ lat: 48.8566, lon: 2.3522 })Builder Pattern
Chain methods to build a Coordinate value incrementally:
All methods return CoordinateBuilder and are chainable.
open.coordinate()
lat: (value: number) => CoordinateBuilder
Set latitude (-90 to 90)
lon: (value: number) => CoordinateBuilder
Set longitude (-180 to 180)
point: (lat: number, lon: number) => CoordinateBuilder
Set both lat and lon at once
from: (value: Coordinate) => CoordinateBuilder
Initialize from existing Coordinate
build: () => Coordinate
Build and validate
Static Methods
parse: (input: unknown) => Coordinate
Parse unknown input (throws on error)
safeParse: (input: unknown) => Result<Coordinate>
Parse unknown input (returns result object)
Properties
lat: number
Latitude in decimal degrees (-90 to 90). Positive is north, negative is south.
lon: number
Longitude in decimal degrees (-180 to 180). Positive is east, negative is west.
Validation
The Coordinate primitive validates:
latmust be a number between -90 and 90 (inclusive)lonmust be a number between -180 and 180 (inclusive)- Neither value can be NaN or Infinity
// Valid
open.coordinate({ lat: 40.7128, lon: -74.0060 }) // New York
open.coordinate({ lat: -33.8688, lon: 151.2093 }) // Sydney
open.coordinate({ lat: 0, lon: 0 }) // Null Island
open.coordinate({ lat: 90, lon: 0 }) // North Pole
open.coordinate({ lat: -90, lon: 0 }) // South Pole
// Invalid - throws Error
open.coordinate({ lat: 91, lon: 0 })
// Error: Invalid Coordinate: lat must be between -90 and 90
open.coordinate({ lat: 0, lon: 181 })
// Error: Invalid Coordinate: lon must be between -180 and 180
open.coordinate({ lat: NaN, lon: 0 })
// Error: Invalid Coordinate: lat must be a valid numberCommon Coordinates
| Location | Latitude | Longitude |
|---|---|---|
| New York | 40.7128 | -74.0060 |
| London | 51.5074 | -0.1278 |
| Tokyo | 35.6762 | 139.6503 |
| Sydney | -33.8688 | 151.2093 |
| Paris | 48.8566 | 2.3522 |
Related
- Primitives Overview - All available primitives
- Bbox - Bounding box using coordinates
- Coordinate Field - Coordinate field type
- Coordinate Schema - JSON Schema definition