计算iPhone中两个纬度和经度之间的航向

时间:2011-05-15 07:36:23

标签: iphone latitude-longitude

  

可能重复:
  how to calculate the current speed and average speed of user travelling from current location to particular loaction in map in iphone

我有一种情况,我不应该使用CLLocationManger类。我有两个纬度和经度值(源和目的地)。我想在iPhone中找出这两个点的标题。我怎么能这样做?

我在这个link中提到了公式。但我无法得到标题。请任何人帮我计算标题

提前谢谢大家。

1 个答案:

答案 0 :(得分:1)

你在这里。这些是可用于计算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;
}

希望它有所帮助。