CLLocationUtils
public final class CLLocationUtils
Utility functions for location calculations and coordinate operations. This class provides common geographic calculations used throughout the OpenCoreLocation framework.
-
Calculates the great-circle distance between two coordinate points using the haversine formula.
The haversine formula determines the shortest distance over the earth’s surface, giving an accurate measurement for most practical purposes.
Example Usage:
let distance = CLLocationUtils.calculateDistance( from: (latitude: 37.7749, longitude: -122.4194), // San Francisco to: (latitude: 40.7128, longitude: -74.0060) // New York ) print("Distance: \(distance) meters") // ~4,134,000 meters
Declaration
Swift
public static func calculateDistance( from: (latitude: Double, longitude: Double), to: (latitude: Double, longitude: Double) ) -> CLLocationDistance
Parameters
from
Starting coordinate (latitude, longitude) in degrees
to
Ending coordinate (latitude, longitude) in degrees
Return Value
Distance in meters
-
Calculates the great-circle distance between two CLLocation objects.
Convenience method that extracts coordinates from CLLocation objects and delegates to the coordinate-based distance calculation.
Declaration
Swift
public static func calculateDistance(from: CLLocation, to: CLLocation) -> CLLocationDistance
Parameters
from
Starting CLLocation
to
Ending CLLocation
Return Value
Distance in meters
-
Calculates the great-circle distance between two CLLocationCoordinate2D objects.
Convenience method for working directly with coordinate structures.
Declaration
Swift
public static func calculateDistance(from: CLLocationCoordinate2D, to: CLLocationCoordinate2D) -> CLLocationDistance
Parameters
from
Starting coordinate
to
Ending coordinate
Return Value
Distance in meters
-
Validates that a coordinate is within valid latitude/longitude ranges.
Declaration
Swift
public static func isValidCoordinate(_ coordinate: CLLocationCoordinate2D) -> Bool
Parameters
coordinate
The coordinate to validate
Return Value
True if the coordinate has valid latitude (-90 to 90) and longitude (-180 to 180)
-
Validates that latitude and longitude values are within valid ranges.
Declaration
Swift
public static func isValidCoordinate(latitude: Double, longitude: Double) -> Bool
Parameters
latitude
Latitude in degrees (-90 to 90)
longitude
Longitude in degrees (-180 to 180)
Return Value
True if both values are within valid ranges
-
Calculates the initial bearing (forward azimuth) from one coordinate to another.
The bearing is the compass direction from the starting point to the ending point, measured in degrees clockwise from true north.
Declaration
Swift
public static func calculateBearing( from: (latitude: Double, longitude: Double), to: (latitude: Double, longitude: Double) ) -> CLLocationDirection
Parameters
from
Starting coordinate
to
Ending coordinate
Return Value
Bearing in degrees (0-360), where 0° is north, 90° is east, etc.
-
Earth’s mean radius in meters
Declaration
Swift
public static let earthRadiusMeters: CLLocationDistance
-
Earth’s mean radius in kilometers
Declaration
Swift
public static let earthRadiusKilometers: CLLocationDistance
-
Maximum valid latitude value in degrees
Declaration
Swift
public static let maxLatitude: CLLocationDegrees
-
Minimum valid latitude value in degrees
Declaration
Swift
public static let minLatitude: CLLocationDegrees
-
Maximum valid longitude value in degrees
Declaration
Swift
public static let maxLongitude: CLLocationDegrees
-
Minimum valid longitude value in degrees
Declaration
Swift
public static let minLongitude: CLLocationDegrees