MTMath

public struct MTMath

Pure math helpers for geographic calculations, projections, and tile indices.

  • Radius of the Earth in meters.

    Declaration

    Swift

    public static let earthRadius: Double
  • Circumference of the Earth in meters.

    Declaration

    Swift

    public static let earthCircumference: Double
  • Maximum latitude used for Web Mercator calculations to prevent infinity.

    Declaration

    Swift

    public static let maxSafeLatitude: Double
  • Converts degrees to radians.

    Declaration

    Swift

    public static func toRadians(degrees: Double) -> Double
  • Converts radians to degrees.

    Declaration

    Swift

    public static func toDegrees(radians: Double) -> Double
  • Converts a longitude to Web Mercator X coordinate in meters.

    Declaration

    Swift

    public static func longitudeToMercatorX(longitude: Double) -> Double
  • Converts a latitude to Web Mercator Y coordinate in meters.

    Declaration

    Swift

    public static func latitudeToMercatorY(latitude: Double) -> Double
  • Converts a WGS84 coordinate to Web Mercator (X, Y) in meters.

    Declaration

    Swift

    public static func wgs84ToMercator(coordinate: CLLocationCoordinate2D) -> (x: Double, y: Double)
  • Converts a Web Mercator X coordinate in meters to longitude.

    Declaration

    Swift

    public static func mercatorXToLongitude(x: Double) -> Double
  • Converts a Web Mercator Y coordinate in meters to latitude.

    Declaration

    Swift

    public static func mercatorYToLatitude(y: Double) -> Double
  • Converts a Web Mercator (X, Y) in meters to WGS84 coordinate.

    Declaration

    Swift

    public static func mercatorToWgs84(x: Double, y: Double) -> CLLocationCoordinate2D
  • Calculates the Web Mercator tile X coordinate for a given longitude and zoom level.

    Declaration

    Swift

    public static func longitudeToTileX(longitude: Double, zoom: Double, round: Bool = true) -> Double
  • Calculates the Web Mercator tile Y coordinate (XYZ scheme) for a given latitude and zoom level.

    Declaration

    Swift

    public static func latitudeToTileY(latitude: Double, zoom: Double, round: Bool = true) -> Double
  • Converts a WGS84 coordinate to Web Mercator tile indices.

    Declaration

    Swift

    public static func wgs84ToTileIndex(
        coordinate: CLLocationCoordinate2D,
        zoom: Double,
        round: Bool = true
    ) -> (x: Double, y: Double)
  • Calculates the great-circle distance between two WGS84 coordinates using the Haversine formula.

    Declaration

    Swift

    public static func haversineDistanceWgs84(from: CLLocationCoordinate2D, to: CLLocationCoordinate2D) -> Double

    Return Value

    The distance in meters.

  • Calculates the total cumulated distance of a route made of multiple WGS84 coordinates.

    Declaration

    Swift

    public static func haversineCumulatedDistanceWgs84(route: [CLLocationCoordinate2D]) -> Double

    Return Value

    The total distance in meters.

  • Computes an intermediate point between two WGS84 coordinates using the Haversine formula.

    Declaration

    Swift

    public static func haversineIntermediateWgs84(
        from: CLLocationCoordinate2D,
        to: CLLocationCoordinate2D,
        ratio: Double
    ) -> CLLocationCoordinate2D

    Parameters

    ratio

    A value between 0.0 and 1.0.

    Return Value

    The intermediate coordinate.

  • Calculates the Earth’s circumference at a given latitude in meters.

    Declaration

    Swift

    public static func circumferenceAtLatitude(latitude: Double) -> Double
  • Wraps a longitude value to be within the [-180, 180] range.

    Declaration

    Swift

    public static func wrapLongitude(_ longitude: Double) -> Double