Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to measure the distance in meters between two CLLocations?

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

like image 920
Ilya Lapan Avatar asked May 03 '12 13:05

Ilya Lapan


2 Answers

CLLocationDistance distance = [aCLLocationA distanceFromLocation:aCLLocationB];
// distance is a double representing the distance in meters
like image 112
Jonas Schnelli Avatar answered Oct 23 '22 12:10

Jonas Schnelli


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...

like image 40
Nitin Avatar answered Oct 23 '22 12:10

Nitin