3

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 given 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.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Sankar Chandra Bose
  • 377
  • 1
  • 12
  • 27

1 Answers1

1

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.

Janak Nirmal
  • 22,706
  • 18
  • 63
  • 99
  • Hi Jennis,thanks for your reply.But here you have used CLLocation Manager class.But my situation is i just have only two set of latitude and longitude which comes from the webservice.I want to findout the distance,heading and bearing...Please let me know your thoughts.. – Sankar Chandra Bose May 15 '11 at 08:28
  • @Sankar you can create CLLocation object with the help of following statement. CLLocation *yourLoc = [[CLLocation alloc] initWithLatitude:yourLatitude longitude:yourLongitude]; – Janak Nirmal May 16 '11 at 04:05
  • Thanks Jennis...I hope this will work..I try to implement this and let you know the feedback..Thanks a lot Jennis for your response – Sankar Chandra Bose May 16 '11 at 08:40
  • @Sankar If it works according to your requirement please mark this as accepted solution or upvote for answer. – Janak Nirmal May 16 '11 at 08:56
  • Ya sure man i will definitely do that... – Sankar Chandra Bose May 16 '11 at 08:59
  • 2
    hey i think what you need is a function to get the heading from two locations. Given an object that has lat,lng attirbutes you could do this: -(float)getHeadingForDirectionToCoordinate:(double)fromLat fromLng:(double)fromLng { float tLat = radians([self.lat floatValue]); float tLng = radians([self.lng floatValue]); float fLat = radians(fromLat); float fLng = radians(fromLng); float brng = atan2(sin(tLng-fLng)*cos(tLat), cos(fLat)*sin(tLat)-sin(fLat)*cos(tLat)*cos(tLng-fLng)); return = fmodf((degrees(brng) + 360), 360); } if someone could reopen the question... – ugiflezet Aug 07 '11 at 17:03