【问题标题】:How to measure the distance in meters between two CLLocations?如何测量两个 CLLocation 之间的距离(以米为单位)?
【发布时间】:2012-05-13 00:08:45
【问题描述】:
如何获得两个CLLocations 之间的距离(以米为单位)? CLLocation 似乎没有提供任何方法来做到这一点。
【问题讨论】:
-
你在开玩笑吗? CLLocation Reference Cmd-F,“米”:“以米为单位的高度” Cmd-G:“不确定性的半径,以米为单位”再次:“以米/秒为单位的瞬时速度”再次:“以米为单位的高度值。”再一次:“distanceFromLocation:返回从接收器位置到指定位置的距离(以米为单位)。”天啊!这在 Xcode 本身中更容易。
标签:
objective-c
cocoa-touch
core-location
cllocation
【解决方案1】:
CLLocationDistance distance = [aCLLocationA distanceFromLocation:aCLLocationB];
// distance is a double representing the distance in meters
【解决方案2】:
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];
希望对你有所帮助...
【解决方案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];
【解决方案4】:
+ (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;
}