How to measure the distance in meters between two CLLocations?
Asked Answered
G

5

19

How can I get the distance in meters between two CLLocations? CLLocation doesn't provide any method to do it, it seeems.

Guttural answered 3/5, 2012 at 13:6 Comment(0)
W
65
CLLocationDistance distance = [aCLLocationA distanceFromLocation:aCLLocationB];
// distance is a double representing the distance in meters
Winther answered 3/5, 2012 at 13:9 Comment(1)
O
5
CLLocationDistance distance = [secondLocation distanceFromLocation:firstLocation];  //      distance is expressed in meters

CLLocationDistance kilometers = distance / 1000.0;
// or you can also use this..
CLLocationDistance meters = distance;

NSString *distanceString = [[NSString alloc] initWithFormat: @"%f", kilometers];

flot totaldistancecovered = [distanceString floatValue];

//Now,you can use this float value for addition...
// distanceMoved  should be float type variable which is declare in .h file...

 distanceMoved = distanceMoved + totaldistancecovered ;
 theLabel.text = [NSString stringWithFormat: @"%f meters", distanceMoved];

Hope, this will help you...

Octahedrite answered 3/5, 2012 at 13:8 Comment(0)
M
3
CLLocation *loc1 = [[CLLocation alloc]  initWithLatitude:dblLatitude longitude:dblLongitude];   
CLLocation *loc2 = [[CLLocation alloc]  initWithLatitude:dblCurrentLatitude longitude:dblCurrentLongitude];
double dMeters = [loc1 distanceFromLocation:loc2];
[loc1 release];
[loc2 release];
Mauro answered 3/5, 2012 at 13:9 Comment(0)
N
3

Swift 3.0

let location1 = CLLocation(latitude: <CLLocationDegrees>, longitude: <CLLocationDegrees>)
let location2 = CLLocation(latitude: <CLLocationDegrees>, longitude: <CLLocationDegrees>)

let distance = location1.distance(from: location2)

distance is in meters as a Double

https://developer.apple.com/reference/corelocation/cllocation/1423689-distance

Noctambulism answered 22/11, 2016 at 20:0 Comment(0)
F
2
+ (CLLocationDistance)distanceBetweenCoordinate:(CLLocationCoordinate2D)originCoordinate andCoordinate:(CLLocationCoordinate2D)destinationCoordinate {    
    CLLocation *originLocation = [[CLLocation alloc] initWithLatitude:originCoordinate.latitude longitude:originCoordinate.longitude];
    CLLocation *destinationLocation = [[CLLocation alloc] initWithLatitude:destinationCoordinate.latitude longitude:destinationCoordinate.longitude];
    CLLocationDistance distance = [originLocation distanceFromLocation:destinationLocation];
    [originLocation release];
    [destinationLocation release];

    return distance;
}
Flyover answered 3/5, 2012 at 13:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.