Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4085726
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:37:15+00:00 2026-05-20T18:37:15+00:00

I am trying to develop a compass for an appliation which has a set

  • 0

I am trying to develop a compass for an appliation which has a set of annotations on a map. I would like to be able to choose an annotation and then have the compass point the user to the chosen location.

I have calculated the degress from the user’s location to the location of the annotation and I have the magnetic heading and the true heading of the iPhone. Also I know how to rotate an image.
But I can’t figure out what the next step is.

The degrees between the user’s location and the annotation location is calculated like this:

    // get user location
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    [locationManager startUpdatingLocation];
    [locationManager startUpdatingHeading];
    CLLocation *location = [locationManager location];
    CLLocationCoordinate2D coordinate = [location coordinate];

    float x1 = coordinate.latitude;
    float y1 = coordinate.longitude;
    float x2 = [annLatitude floatValue];
    float y2 = [annLongitude floatValue];

    float dx = (x2 - x1);
    float dy = (y2 - y1);

    if (dx == 0) {
        if (dy > 0) {
            result = 90;
        }
        else {
            result = 270;
        }
    }
    else {
        result = (atan(dy/dx)) * 180 / M_PI;
    }

    if (dx < 0) {
        result = result + 180;
    }

    if (result < 0) {
        result = result + 360;
    }

The true and the magnetic heading is retrieved like this:

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
    arrow.transform = CGAffineTransformMakeRotation(newHeading.magneticHeading);

    // NSLog(@"magnetic heading: %f", newHeading.magneticHeading);
    // NSLog(@"true heading: %f", newHeading.trueHeading);
}

Can anyone tell me what I should do now to make the arrow point to the location – even if the iPhone is rotated?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T18:37:15+00:00Added an answer on May 20, 2026 at 6:37 pm

    I had time to play with this again.

    This will do it:

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self; 
        locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
        locationManager.distanceFilter = kCLDistanceFilterNone; 
        [locationManager startUpdatingLocation];
        [locationManager startUpdatingHeading];
    
        CLLocation *location = [locationManager location];
        CLLocationCoordinate2D user = [location coordinate];
    
        [self calculateUserAngle:user];
    }
    
    -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
        CLLocationCoordinate2D here =  newLocation.coordinate;
    
        [self calculateUserAngle:here];
    }
    
    - (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
        compass.transform = CGAffineTransformMakeRotation(newHeading.magneticHeading * M_PI / 180);
        needle.transform = CGAffineTransformMakeRotation((degrees - newHeading.magneticHeading) * M_PI / 180);
    }
    
    -(void) calculateUserAngle:(CLLocationCoordinate2D)user {
        locLat = [[targetLocationDictionary objectForKey:@"latitude"] floatValue];
        locLon = [[targetLocationDictionary objectForKey:@"longitude"] floatValue];
    
        NSLog(@"%f ; %f", locLat, locLon);
    
        float pLat;
        float pLon;
    
        if(locLat > user.latitude && locLon > user.longitude) {
            // north east
    
            pLat = user.latitude;
            pLon = locLon;
    
            degrees = 0;
        }
        else if(locLat > user.latitude && locLon < user.longitude) {
            // south east
    
            pLat = locLat;
            pLon = user.longitude;
    
            degrees = 45;
        }
        else if(locLat < user.latitude && locLon < user.longitude) {
            // south west
    
            pLat = locLat;
            pLon = user.latitude;
    
            degrees = 180;
        }
        else if(locLat < user.latitude && locLon > user.longitude) {
            // north west
    
            pLat = locLat;
            pLon = user.longitude;
    
            degrees = 225;
        }
    
        // Vector QP (from user to point)
        float vQPlat = pLat - user.latitude;
        float vQPlon = pLon - user.longitude;
    
        // Vector QL (from user to location)
        float vQLlat = locLat - user.latitude;
        float vQLlon = locLon - user.longitude;
    
        // degrees between QP and QL
        float cosDegrees = (vQPlat * vQLlat + vQPlon * vQLlon) / sqrt((vQPlat*vQPlat + vQPlon*vQPlon) * (vQLlat*vQLlat + vQLlon*vQLlon));
        degrees = degrees + acos(cosDegrees);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to develop a simple map application, which will display a map
I am trying to develop an XSLT stylesheet which will transform an xml into
I am trying develop an Android app which uses Google maps. So for the
I have been trying to develop an app for my ipad which i can
I am trying to develop a Qt App using 4.7.3 which involves the writing
I'm trying to develop an app which first checks if the device is connected
Am trying to develop a windows service which listens to a rabbitMQ listener and
i am trying to develop an android app (named ContactListFinder) which follows the following
I'm trying to develop a responsive navigation menu which dynamically creates a More.. menu
I am trying to develop a concept for a simple algorithm which will turn

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.