如何测量两个CLLococ之间的距离(单位为米)?

问题描述:

如何获得两个CLLocation之间的距离(米)? CLLocation不提供任何方法来做到这一点,它seeems。如何测量两个CLLococ之间的距离(单位为米)?

+5

你是在开玩笑吗? [CLLocation Reference](http://developer.apple.com/library/ios/#documentation/CoreLocation/Reference/CLLocation_Class/CLLocation/CLLocation.html)Cmd-F,“米”:“以米为单位测量的高度”Cmd -G:“以米为单位测量的不确定性半径”再次说明:“以米/秒为单位的瞬时速度”再次说明:“以米为单位的高度值的准确度。”再一次:“distanceFromLocation: 返回从接收者位置到指定位置的距离(以米为单位)。”天啊!这在Xcode本身中更容易。 –

+1

谢谢你,我的坏。 –

CLLocationDistance distance = [aCLLocationA distanceFromLocation:aCLLocationB]; 
// distance is a double representing the distance in meters 
+2

http://developer.apple.com/library/ios/DOCUMENTATION/CoreLocation/Reference/CLLocation_Class/CLLocation/CLLocation.html#//apple_ref/doc/UID/TP40007126-CH3-SW18 – JeremyP

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

希望,这将帮助你......

+0

我无法添加代码块。请添加一些代码块。 – Nit

+ (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; 
} 

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

雨燕3.0

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

let distance = location1.distance(from: location2) 

distance是以米为单位的Double

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