Pheepster
Pheepster

Reputation: 6347

Calculate offset between two CLLocationCoordinate2D locations

I need to determine offset in degrees lat/lon between two CLLocation objects. I have seen many examples of how to calculate a new location based on a distance and bearing but this is not what I need.

In my case, the two locations are known and what I am trying to obtain is the CLLocationDegrees latitude and CLLocationDegrees longitude between the two known locations in order to have an offset to apply to other coordinates.

class func getDistancesInDegrees(origin:CLLocationCoordinate2D, destination:CLLocationCoordinate2D) -> (degLat: CLLocationDegrees, degLon:CLLocationDegrees) {
    var latidueDegrees:CLLocationDegrees = 0.0
    var longitudeDegrees:CLLocationDegrees = 0.0

    //...

    return (degLat: latidueDegrees, degLon:longitudeDegrees)
}

Does anybody know of a good example of how to accomplish this? Thanks!

Upvotes: 0

Views: 1199

Answers (2)

PGDev
PGDev

Reputation: 24341

If you are using google maps sdk, you can use GMSGeometryDistance to find the distance between 2 locations.

let distance = GMSGeometryDistance(origin, destination)

Refer https://developers.google.com/maps/documentation/ios-sdk/reference/group___geometry_utils.html#ga423e751bcbe4ea974049f4b78d90a86b

Upvotes: 3

Damien
Damien

Reputation: 3362

If I understand, what you're trying to get is the distance between 2 known location in degrees ?

If this is it then try :

class func getDistancesInDegrees(origin:CLLocationCoordinate2D, destination:CLLocationCoordinate2D) -> (degLat: CLLocationDegrees, degLon:CLLocationDegrees) {
    var latidueDegrees:CLLocationDegrees = Double(origin.coordinate.latitude) - Double(destination.coordinate.latitude)
    var longitudeDegrees:CLLocationDegrees = Double(origin.coordinate.longitude) - Double(destination.coordinate.longitude)

    return (degLat: latidueDegrees, degLon:longitudeDegrees)
}

Upvotes: 1

Related Questions