2011-05-15 102 views
3

可能重複:
how to calculate the current speed and average speed of user travelling from current location to particular loaction in map in iphone計算iPhone 2經度和緯度之間的航向

我有一個情況我不應該使用CLLocationManger class.I我有兩個緯度和經度給出值(來源和目的地)。我想從iPhone中的這兩點找出標題。我該如何做?

我提到的公式在此link。但我不能夠得到heading.Please任何機構幫助我在計算標題

謝謝大家提前。

+0

爲什麼這個問題被認爲是重複的?鏈接的重複問題不計算標題。 – franklins 2012-11-16 16:59:32

回答

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; 
} 

希望它有幫助。

+0

嗨Jennis,謝謝你的答覆。但在這裏你已經使用了CLLocation Manager類。但是我的情況是我只有兩組來自webservice的緯度和經度。我想找出距離,標題和方位。請讓我知道你的想法.. – 2011-05-15 08:28:39

+0

@Sankar你可以在下面的語句幫助下創建CLLocation對象。 CLLocation * yourLoc = [[CLLocation alloc] initWithLatitude:yourLatitude longitude:yourLongitude]; – 2011-05-16 04:05:33

+0

謝謝珍妮斯...我希望這將工作..我試圖實現這一點,並讓你知道反饋..感謝很多珍妮斯你的迴應 – 2011-05-16 08:40:48

相關問題