如何使用MapKit框架中的distanceFromLocation方法计算两个位置之间的英里数。
谢谢
谢谢
尝试以下代码:
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。有关详细信息,请参见 此主题。