3

How can we point to a specific location coordinate regardless the device heading.I have tried Haversine Formula to compute bearing between two points and adding device heading to that angle.But still it create issues.

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    degrees = [self bearingToLocation:newLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
    {
        float direction = newHeading.magneticHeading;
        if (direction<0) {
            return;
        }
        if (direction > 180)
        {
            direction = degrees+direction;
            direction = 360 - direction;
        }
        else
        {
            direction = degrees+direction;
            direction = 0 - direction;
        }
        // Rotate the arrow image
        if (self.delegate && [self.delegate respondsToSelector:@selector(compassAngleChange:)]) {
            [self.delegate compassAngleChange:direction];
        }
}
-(double) bearingToLocation:(CLLocation *) destinationLocation {

    double lat1 = DegreesToRadians(destinationLocation.coordinate.latitude);
    double lon1 = DegreesToRadians(destinationLocation.coordinate.longitude);

    double lat2 = DegreesToRadians(self.latitudeOfTargetedPoint);
    double lon2 = DegreesToRadians(self.longitudeOfTargetedPoint);

    double dLon = lon2 - lon1;

    double y = sin(dLon) * cos(lat2);
    double x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon);
    double radiansBearing = atan2(y, x);

    return RadiansToDegrees(radiansBearing);
}
Pawan Rai
  • 3,434
  • 4
  • 32
  • 42
  • Firstly, whats exactly is the issue? which part of this code does not work? Second, do not use the deprecated methods of location delegate (didUpdateToLocation: fromLocation). – Pawan Rai Mar 15 '17 at 15:21
  • actually i am not able to get accurate angle ,and the given code is all about finding an angle between two locations with respect to device heading .this is the issue. – Zeeshan Suleman Mar 16 '17 at 05:44

0 Answers0