rakeshNS
rakeshNS

Reputation: 4257

Finding distance between CLLocationCoordinate2D points

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.

Upvotes: 50

Views: 32094

Answers (8)

Eric Yuan
Eric Yuan

Reputation: 1502

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)
    }
}

Upvotes: 27

Ralf Ebert
Ralf Ebert

Reputation: 4092

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

Upvotes: 2

Ramis
Ramis

Reputation: 16639

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)

Upvotes: 1

Aviel Gross
Aviel Gross

Reputation: 9975

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)
    }
}

Upvotes: 37

Alex Chase
Alex Chase

Reputation: 1091

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)

Upvotes: -2

Apurv
Apurv

Reputation: 17186

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];

Upvotes: 42

VBaarathi
VBaarathi

Reputation: 803

    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.

Upvotes: 7

Valerii Lider
Valerii Lider

Reputation: 1814

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

Upvotes: 36

Related Questions