1

I would like to find the distance to a location and which direction the device is heading in relation to that location. I've got the distance working but cant work out how to find the which way the location.

_theLocation = [[CLLocation alloc]initWithLatitude:-41.561004 longitude:173.849030];

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    CLLocationDistance meters = [newLocation distanceFromLocation:_theLocation] * 0.000621371192;
}

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
    NSString *str = [NSString stringWithFormat:@"Heading = %.f", newHeading.trueHeading];  
}  

Is it possible to find the heading to _theLocation from our location?

daihovey
  • 3,653
  • 13
  • 62
  • 103
  • 1
    You need to calculate the "bearing" between two coordinates. There's no built-in function for it so need to write your own. See [this](http://stackoverflow.com/questions/3198186/calculate-compass-heading-to-a-cllocation-haversine-functions-for-ios) and [this](http://stackoverflow.com/questions/3925942/cllocation-category-for-calculating-bearing-w-haversine-function). –  Aug 17 '11 at 12:56
  • Thanks will look into these links. – daihovey Aug 17 '11 at 13:21

1 Answers1

0

Can't you simply capture the device's location at two instances & figure out the direction wrt the location?

Not a complete solution, but assuming that the device only moves along the line joining the device & the location, you can use – distanceFromLocation:. If you capture the distance from the location at two instances & see if the latter value is less than the former, you can deduce that the device has moved closer to the location.

In fact, from your question, it appears that you want to determine whether the device has moved closer to the location or not (and not the exact direction, say N or NE). If that's the case, then this should work.

HTH,

Akshay

Akshay
  • 5,699
  • 3
  • 21
  • 35