你在這裏。這些是您可以用來計算2個位置之間距離的2個功能。
-(float)getDistanceInKm:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
float lat1,lon1,lat2,lon2;
lat1 = newLocation.coordinate.latitude * M_PI/180;
lon1 = newLocation.coordinate.longitude * M_PI/180;
lat2 = oldLocation.coordinate.latitude * M_PI/180;
lon2 = oldLocation.coordinate.longitude * M_PI/180;
float R = 6371; // km
float dLat = lat2-lat1;
float dLon = lon2-lon1;
float a = sin(dLat/2) * sin(dLat/2) + cos(lat1) * cos(lat2) * sin(dLon/2) * sin(dLon/2);
float c = 2 * atan2(sqrt(a), sqrt(1-a));
float d = R * c;
NSLog(@"Kms-->%f",d);
return d;
}
-(float)getDistanceInMiles:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
float lat1,lon1,lat2,lon2;
lat1 = newLocation.coordinate.latitude * M_PI/180;
lon1 = newLocation.coordinate.longitude * M_PI/180;
lat2 = oldLocation.coordinate.latitude * M_PI/180;
lon2 = oldLocation.coordinate.longitude * M_PI/180;
float R = 3963; // km
float dLat = lat2-lat1;
float dLon = lon2-lon1;
float a = sin(dLat/2) * sin(dLat/2) + cos(lat1) * cos(lat2) * sin(dLon/2) * sin(dLon/2);
float c = 2 * atan2(sqrt(a), sqrt(1-a));
float d = R * c;
NSLog(@"Miles-->%f",d);
return d;
}
希望它有幫助。
爲什麼這個問題被認爲是重複的?鏈接的重複問題不計算標題。 – franklins 2012-11-16 16:59:32