开发者

Calculating heading between two latitude and longitude in iPhone [duplicate]

开发者 https://www.devze.com 2023-03-06 12:36 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: how to calculate the current speed and average speed of user travelling from current location to particular l
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

how to calculate the current speed and average speed of user travelling from current location to particular loaction in map in iphone

I have a situation where i should not use CLLocationManger class.I am g开发者_如何学Pythoniven with two latitude and longitude values(a source and destination).I want find out the heading from these two points in iPhone.How can i do this?

I referred to the formula in this link.But i am not able to get the heading.Please any body help me in calculating the heading

Thank you all in advance.


Here you are. These are 2 functions you can use to calculate distance between 2 Locations.

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

Hope it helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消