Get started
Swift SDK

Geo and navigation

Geo without a map

MountayaGeo holds the rules the map and its elements draw by, as plain functions. It calls no network and draws nothing, so it runs in a test, a widget or a server.

Measure and format distances

A position is [longitude, latitude], as on the wire:

import MountayaGeo

// Chamonix to Argentière.
let line: [[Double]] = [[6.8712, 45.9231], [6.9271, 45.9822]]
let meters = routeLengthMeters(line)

let units = UnitPreferences(language: "fr-FR")
print(formatDistance(meters, units: units), formatLngLat(line[0]) ?? "")

Every value is written for its reader, in the language and units you pass, never the device's.

Build an elevation series

The series every profile and table draws from, and its totals:

import MountayaClient
import MountayaGeo

func describe(_ itinerary: RestSchema.ItineraryWithDetails) -> String {
  let series = buildElevationSeries(ResolvedItinerary(itinerary).geometry)
  let totals = profileTotals(series)
  let units = UnitPreferences(language: "en")
  return "\(formatElevation(totals.ascent, units: units)) up, "
    + "\(formatElevation(totals.descent, units: units)) down"
}

Read the terrain at a point

The terrain under a point is read off the Tile API's elevation tiles. Geo says which tiles to fetch and what their pixels mean:

import MountayaGeo

let source = ElevationSource(encoding: .mapbox, tileSize: 512, zoom: 15)
let tiles = elevationTilesAt([6.8712, 45.9231], source: source)

let height = decodeElevation([1, 134, 160], encoding: .mapbox)
let facing = aspectCardinal(236.3)
print(tiles.count, height ?? 0, facing?.rawValue ?? "")

Fetch the tiles with your client, then hand their pixels to terrainFromElevationTiles(_:source:tiles:) for the height, slope and aspect.

Units and languages

UnitPreferences carries the language and units every figure is written in:

import MountayaClient
import MountayaGeo

let imperial = UnitPreferences(language: "en-US", length: .imperial)
print(formatDistance(42_195, units: imperial), formatSnowfall(35, units: imperial))

let french = scopeOf(.standard, stating: ElementsScopeStatement(language: .fr, locale: "fr-CH"))
print(french.word("controls.zoomIn"))

Next steps

Last updated