Resolved Compass question

Discussion in 'iOS Programming' started by xArtx, Feb 14, 2013.

  1. xArtx, Feb 14, 2013
    Last edited: Feb 15, 2013

    xArtx macrumors 6502a

    Mar 30, 2012
    I'm getting the heading from the iPhone compass with code found in some sample:
    - (void)locationManager:(CLLocationManager *)manager
           didUpdateHeading:(CLHeading *)newHeading
        heading = newHeading.trueHeading;
    What I have found is that when the device is moving, the function is called
    many times per second, and puts a measureable load on the CPU.

    Getting GPS coordinates doesn't present the same issue since you get
    updates no more than once per second.

    I only care to know the change in heading every second.
    Can the new heading be queried manually with a timer, or some other way?
    Cheers, Art.
  2. ArtOfWarfare macrumors G3


    Nov 26, 2007
    Maybe you can just turn the location updates on and off?
  3. xArtx thread starter macrumors 6502a

    Mar 30, 2012
        [locationManager startUpdatingHeading];    
        [locationManager startUpdatingLocation]; 
    Location isn't a problem, just the heading update,
    but since you have to turn them both on separately,
    it looks like you can turn it off as well so that should work, thanks :)

    For anyone else's info, this congestion only occurs when you're spinning the device,
    not so much when you're just driving or walking.
  4. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    Are you testing your app on a spinning tea cup ride or something? ;)
  5. xArtx, Feb 15, 2013
    Last edited: Feb 15, 2013

    xArtx thread starter macrumors 6502a

    Mar 30, 2012
    Lol, no, using the compass to rotate a map if the user is not moving.
    The map can't render nearly as fast as compass updates come in though,
    and receiving too many updates is actually slowing down the render,
    so no point in receiving all updates...
  6. ArtOfWarfare macrumors G3


    Nov 26, 2007
    It seems to me your issue is you're linking updates from the compass directly to having the map update. You can have the same issue in games if you try to redraw every time a finger moves or the device is rotated. You need to debounce your input... have all of your input go to a function that looks at how long its been since it last passed on updated data and have it reject the data if its been fewer than 0.02 seconds or whatever amount of time you find it takes to redraw the map.
  7. xArtx thread starter macrumors 6502a

    Mar 30, 2012
    Nope, already, the screen can't be redrawn until the current one is complete.
    the rotation (angle) variable is only copied over from the last heading update at that stage.

    It's not a huge problem though.
    Since you mentioned it... I realised I can turn off the compass the whole time
    the iPhone is moving, because then it calculates it's heading from GPS
    coordinates like commercial GPS units.

    I notice the effect of fast heading updates (spinning the iPhone) most in
    a dynamic clock display with digits that roll like an odometer which is also present in the program.

Share This Page