49

I know from documentation we can find distance between two CLLocation points using the function, distanceFromLocation:. But my problem is I dont have CLLocation data type with me, I have the CLLocationCoordinate2D points. So how can I find distance between two CLLocationCoordinate2D points. I have seen the post post but not helpful for me.

Community
  • 1
  • 1
rakeshNS
  • 4,227
  • 4
  • 28
  • 42
  • Distance calculations are actually quite tricky. You should really have a second look at CLLocation. – David Rönnqvist Jun 18 '12 at 05:42
  • do u have lat and long then create cllocation and then use CLLocationDistance meters = [newLocation distanceFromLocation:oldLocation]; – Paresh Navadiya Jun 18 '12 at 05:43
  • Check this : [distanceFromLocation - Calculate distance between two points][1] [1]: http://stackoverflow.com/questions/3905896/distancefromlocation-calculate-distance-between-two-points – webmastx Jun 18 '12 at 05:52

8 Answers8

41

You should create an object of CLLocation using,

- (id)initWithLatitude:(CLLocationDegrees)latitude
    longitude:(CLLocationDegrees)longitude;

Then, you should be able to calculate the distance using

[location1 distanceFromLocation:location2];
Cris
  • 774
  • 9
  • 32
Apurv
  • 17,116
  • 8
  • 51
  • 67
  • 5
    With code CLLocationDistance distance = [aCLLocationA distanceFromLocation:aCLLocationB]; // distance is a double representing the distance in meters (Source - http://stackoverflow.com/a/10432069/1364174) – Paweł Brewczynski Jul 24 '14 at 07:31
  • No needs to create CLLocation for getting distance between points. Please look at my answer at the bottom – Valerii Lider Jun 19 '15 at 11:39
37

Swift 5:

extension CLLocation {
    
    /// Get distance between two points
    ///
    /// - Parameters:
    ///   - from: first point
    ///   - to: second point
    /// - Returns: the distance in meters
    class func distance(from: CLLocationCoordinate2D, to: CLLocationCoordinate2D) -> CLLocationDistance {
        let from = CLLocation(latitude: from.latitude, longitude: from.longitude)
        let to = CLLocation(latitude: to.latitude, longitude: to.longitude)
        return from.distance(from: to)
    }
}
Alexander Volkov
  • 7,904
  • 1
  • 47
  • 44
Aviel Gross
  • 9,770
  • 3
  • 52
  • 62
  • 9
    But I think more like this: extension CLLocationCoordinate2D { // In meteres func distance(#to:CLLocationCoordinate2D) -> CLLocationDistance { let from = CLLocation(latitude: self.latitude, longitude: self.longitude) let to = CLLocation(latitude: to.latitude, longitude: to.longitude) return from.distanceFromLocation(to) } } – kwerle May 04 '15 at 16:31
33

If it is ok for you to get distance in meters between points, then

CLLocationCoordinate2D coordinate1 = <some value>
CLLocationCoordinate2D coordinate2 = <some value>
…
MKMapPoint point1 = MKMapPointForCoordinate(coordinate1);
MKMapPoint point2 = MKMapPointForCoordinate(coordinate2);
CLLocationDistance distance = MKMetersBetweenMapPoints(point1, point2);

will return the distance between two points. No needs to create CLLocation by given CLLocationCoordinate2D. This defines are available since iOS 4.0

Valerii Lider
  • 1,774
  • 16
  • 21
25

Swift5: combined above thoughts

extension CLLocationCoordinate2D {
    //distance in meters, as explained in CLLoactionDistance definition
    func distance(from: CLLocationCoordinate2D) -> CLLocationDistance {
        let destination=CLLocation(latitude:from.latitude,longitude:from.longitude)
        return CLLocation(latitude: latitude, longitude: longitude).distance(from: destination)
    }
}
Eric Yuan
  • 1,213
  • 11
  • 13
6
    let point1 = MKMapPointForCoordinate(myLocation)
    let point2 = MKMapPointForCoordinate(friendLocation)
    let distance = MKMetersBetweenMapPoints(point1, point2)/1000
    let distanceStr = NSString(format: "%.3f", distance)

Simple version of Valleri's answer. Divide by 1000 to get KM followed by conversion to string.

VBaarathi
  • 793
  • 8
  • 12
2

CLLocationCoordinate2D+Distance.swift for Swift 5.5 based on MapKit:

import MapKit

extension CLLocationCoordinate2D {

    /// Returns the distance between two coordinates in meters.
    func distance(to: CLLocationCoordinate2D) -> CLLocationDistance {
        MKMapPoint(self).distance(to: MKMapPoint(to))
    }

}

Version based on CLLocation:

import CoreLocation

extension CLLocationCoordinate2D {

    /// Returns the distance between two coordinates in meters.
    func distance(to: CLLocationCoordinate2D) -> CLLocationDistance {
        CLLocation(latitude: latitude, longitude: longitude)
            .distance(from: CLLocation(latitude: to.latitude, longitude: to.longitude))
    }

}

Both extensions have a performance overhead which might be relevant if processing a large amount of coordinates. In those cases:

There is an extension to CLLocationCoordinate2D in the Euclid package which performs the computation directly, see: distance(to:)

Ralf Ebert
  • 3,556
  • 3
  • 29
  • 43
1

Swift 5: CLLocationCoordinate2D extension to calculate distance:

extension CLLocationCoordinate2D {
    /// Returns distance from coordianate in meters.
    /// - Parameter from: coordinate which will be used as end point.
    /// - Returns: Returns distance in meters.
    func distance(from: CLLocationCoordinate2D) -> CLLocationDistance {
        let from = CLLocation(latitude: from.latitude, longitude: from.longitude)
        let to = CLLocation(latitude: self.latitude, longitude: self.longitude)
        return from.distance(from: to)
    }
}

Usage:

let coordinate = CLLocationCoordinate2D(latitude: 11, longitude: 39)
let from = CLLocationCoordinate2D(latitude: 30, longitude: 43)
let distance = coordinate.distance(from: from)
Ramis
  • 13,985
  • 7
  • 81
  • 100
-2

CLLocationDistance is a measurement in meters.

let point1 = CLLocationCoordinate2D(latitude: lat1, longitude: lon1)
let point2 = CLLocationCoordinate2D(latitude: lat2, longitude: lon2)
let distance = point1.distance(to: point2)
Alex Chase
  • 960
  • 1
  • 7
  • 11
  • 2
    There's no need to import another framework (MapKit) just for calculating distances. `CLLocation` *does* have a `distance(from:)` method that calculates distances between two `CLLocation` objects, and it's pure CoreLocation. – Alejandro Iván Aug 23 '19 at 15:06
  • Swift 4+: Just use let distance = aLocation.distance(from: anotherLocation) https://developer.apple.com/documentation/corelocation/cllocation/1423689-distance – MartijndeM Jun 18 '20 at 07:11