如何得到目标c中两个地点之间的距离,单位是英里



如何使用mapkit框架的distanceFromLocation方法计算两个位置之间的距离(以英里为单位)

谢谢

这个问题在这里已经有了答案。现在您只需要将米转换为英里,即:

1 Meter = 0.000621371192 Miles 

1 Mile = 1609.344 Meters 

试试下面的代码:

double latA = [currentlat floatValue];
double logA = [currentlong floatValue];
double latB = [toLat floatValue];
double logB = [toLong floatValue];
CLLocation *locA = [[CLLocation alloc] initWithLatitude:latA
                                              longitude:logA];
CLLocation *locB = [[CLLocation alloc] initWithLatitude:latB longitude:logB];
CLLocationDistance distance = [locA distanceFromLocation:locB];
NSLog(@"Calculated Miles %@", [NSString stringWithFormat:@"%.1fmi",(distance/1609.344)]);

这个功能在苹果的文档中似乎是不言自明的。它会给出用户位置和给定位置之间的距离,所以你需要创建一个新的CLLocation对象来告诉iPhone你想去哪里。

函数以米为单位给出结果。要转换为英里,请乘以0.000621371192。

最新更新