47

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.

3
  • Distance calculations are actually quite tricky. You should really have a second look at CLLocation. Jun 18, 2012 at 5:42
  • do u have lat and long then create cllocation and then use CLLocationDistance meters = [newLocation distanceFromLocation:oldLocation]; Jun 18, 2012 at 5:43
  • Check this : [distanceFromLocation - Calculate distance between two points][1] [1]: stackoverflow.com/questions/3905896/…
    – webmastx
    Jun 18, 2012 at 5:52

8 Answers 8

40

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];
2
  • 5
    With code CLLocationDistance distance = [aCLLocationA distanceFromLocation:aCLLocationB]; // distance is a double representing the distance in meters (Source - stackoverflow.com/a/10432069/1364174) Jul 24, 2014 at 7:31
  • No needs to create CLLocation for getting distance between points. Please look at my answer at the bottom Jun 19, 2015 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)
    }
}
1
  • 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 4, 2015 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

1
  • 5
    Good, but needs MapKit to be linked. Using CLLocation and then distance doesn't need it. Apr 26, 2018 at 12:40
24

Swift4: 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)
    }
}
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.

0
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:)

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)
-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)
2
  • 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. Aug 23, 2019 at 15:06
  • Swift 4+: Just use let distance = aLocation.distance(from: anotherLocation) developer.apple.com/documentation/corelocation/cllocation/…
    – MartijndeM
    Jun 18, 2020 at 7:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.