daidai daidai - 3 months ago 9
Objective-C Question

Find if device is pointing in the right heading / direction?

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?

Answer

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

Comments