3

Can the iPhone determine if you're facing north, south, east or west?

Chris Hanson
  • 54,380
  • 8
  • 73
  • 102
Rhett
  • 119
  • 1
  • 7
  • Would you have to be holding it? – Nosredna Jul 24 '09 at 22:24
  • True or magnetic? There's a considerable difference. About 12.5 degrees where I am right now. –  Jul 28 '09 at 23:08
  • Hi There. i'm facing the exact same problem: i need to compute true north based on the position of a user. This app should be usable on iphone 3G. i've read the anwsers bellow, but don't understand it much. did you ever found a solution? – clide313 Jun 29 '10 at 09:12

5 Answers5

7

If you're developing for 2.2 you might want to take a look at the following additions to CoreLocation:

CLLocation.course

CLLocation.speed

CLLocationDirection

CLLocationSpeed

CLLocation.course will give you heading which will allow you to determine north/south/east/west.

Anne Porosoff
  • 1,931
  • 16
  • 18
  • I understand direction and course are related and come from the GPS data. You can only obtain direction/course if the GPS detects you're moving. – Adam Hawes Jan 25 '09 at 07:10
  • This is true, the heading information being returned is not going to be incredibly precise and does require speed in order to work. – Anne Porosoff Feb 02 '09 at 18:38
  • 4
    3G S has a digital compass. It gives you magnetic north. If you turn on the GPS, it can also give you true north. – Erich Mirabal Jul 23 '09 at 19:19
2

I'm not so sure, but here are some options:

Gather two or more data points over 3-10 seconds. Plot out, with GPS, where the motion is tending towards. That could be your direction.

Also, you could use the accelerometer to determine which direction (left, right, forwards, backwards) the iPhone itself is moving. Pair this up with the previous data, and you may have a more accurate reading.

These aren't great, because GPS isn't as sensitive as you'd like, and Accelerometers are probably more sensitive than you'd like, and both depend on motion. But they may work.

stalepretzel
  • 15,543
  • 22
  • 76
  • 91
  • That's the way I'm afraid I'll have to go--hoping there's some magic compass in there. My Blackberry 8800 has a compass app and based on it's performance I always suspected that the BB needed to be in motion (trending GPS coordinates) for it to determine which direction the device was headed in. – Rhett Jan 05 '09 at 04:27
2

There's no compass in the iPhone or iPhone 3G. The iPhone 3GS, however, has seen the addition of a digital compass, so this process has become easier.

On a 1st gen or 3G, the only way to determine facing is what stalepretzel is suggesting. But beware that it will only work when the GPS has a sufficient lock and the user is actually moving. You'll have to do a lot of smoothing to get any kind of usable data, perhaps with instructions to the user to hold it still while walking.

lc.
  • 113,939
  • 20
  • 158
  • 187
  • There is a compass in the iPhone 3GS – Greg Beech Jul 23 '09 at 19:07
  • @Erich Mirabal - Please read the timestamp on the answer. The 3GS did not exist when this was posted. I have, however edited my answer to reflect this now. – lc. Jul 27 '09 at 01:09
  • @Greg Beech - Very true and I have updated my answer to reflect this. Such is the problem with ever-changing technology. – lc. Jul 27 '09 at 01:16
  • @lc - I apologize. It is so easy for this stuff to become outdated. But, I guess the downvote helps to keep it as a living document. +1 now. – Erich Mirabal Jul 27 '09 at 15:27
  • @Erich Mirabal - No worries and I don't disagree that it's good to keep these things alive. It's just pretty hard to keep track of it all. – lc. Jul 28 '09 at 03:13
0

This maybe a bit of a hack but what I was doing was keeping tracking of the lat/long and then determining by that the direction.

Within my code that returns the GPS information I created a delegate that would return back the latitude and longitude

[[self delegate] currentDirectionLat:newLocation.coordinate.latitude Long:newLocation.coordinate.longitude];

The code that was consuming the delegate would then have a method like so to get the coordinates and then set a label on the screen to the direction.

-(void)currentDirectionLat:(float)latitude Long:(float)longitude
{
    prevLongitude = currentLongitude;
    prevLatitude  = currentLatitude;

    currentLongitude = longitude;
    currentLatitude = latitude;

    NSString *latDirection = @"";
    if (currentLatitude > prevLatitude)
        latDirection = @"N";
    else if (currentLatitude < prevLatitude)
        latDirection = @"S";

    NSString *longDirection = @"";
    if (currentLongitude > prevLongitude)
        longDirection = @"E";
    else if (currentLongitude < prevLongitude)
        longDirection = @"W";

    if ([longDirection length] > 0)
        [lblDirection setText:[NSString stringWithFormat:@"%@%@",latDirection,longDirection]];  
}

I've not 100% tested this out but seems to have worked on my one application I was writing. If you want more information please let me know and I can forward you my code that I've done on it.

You will also not get a good reading until the iPhone is moving to get the lat and long change. However, this does not take much to get it to change!

Niels Hansen
  • 1,546
  • 2
  • 13
  • 18
  • Please give me some code, I cant figure out from this. Suppose we have two latitude and longitude ex.First Our location and then second location then wt can i do? – Mehul Mistri Jul 18 '11 at 06:39
-1

To my knowledge there is no compass in the iPhone 3G. Unfortunately, unless I'm missing something I don't think looking at GPS and accelerometer data will help you determine which direction the iPhone is facing - only the direction you're travelling. Sorry.

UPDATE: Note that the question and this post was written when iPhone 3G was the current model, since then Apple has released the iPhone 3GS which introduced a compass.

CodeAndCats
  • 7,508
  • 9
  • 42
  • 60
  • Interesting: I've just done a search for Compass in the App Store and there are a couple apps which use the current time and the position of the sun to work out which way you're facing. Atleast one other app uses GPS while you're moving to determine travel direction. – CodeAndCats Jan 05 '09 at 04:45
  • @Erich Mirabal - If you took the time to read the timestamp on this answer, you will learn **info on the 3GS was not publicly available on January 5, 2009**. Your comment is, frankly, out of line. Yes, this answer has been made obsolete by the introduction of the 3GS, but it was correct at the time and is has been up-voted a long time ago as well. – lc. Jul 27 '09 at 01:14
  • @lc (and Ben) - I do apologize. My brain did not even register the date. Still, if the guy does not know and is just guessing, why answer? – Erich Mirabal Jul 27 '09 at 15:31
  • Erich, nowhere in my post did I say "I guessed", based on my knowledge I was pretty sure about my answer and despite your uninformed criticism my post stands correct. I don't think it deserves a down vote simply because I wasn't arrogantly sure of myself enough for your liking. – CodeAndCats Jul 28 '09 at 04:00
  • Ben, I tried to undo the down-vote but can't until you edit your answer (the system won't let me cause it is too old). It has nothing to do with arrogance, just the level of wishy-washy talk makes it seem like it is a non-answer. Again, I am sorry for the un-deserved downvote. I still stand by my opinion that your answer is non-committal and feels wishy-washy without adding any technical knowledge. As soon as you edit it I will be able to undo the down-vote. – Erich Mirabal Jul 28 '09 at 19:31
  • Absence of my absolute certainty aside, if you read my post again you'll see I also elaborated that taking accelerometer data and/or GPS coordinate history which other people suggested will not give you the direction the iPhone is facing but only the direction of travel. This was a critical point to raise given the question asks which way you're facing, not traveling. So I do feel like it was a worthy contribution and that you are being a bit critical. I understand you've found incorrect answers in the past & appreciate your willingness to undo your downvote - I've updated my answer. :) – CodeAndCats Jul 28 '09 at 23:30
  • Done and done. I'm sorry for being a jackass. I never meant to be one (but I was, alas). Anyhoo... Vote was taken back. How's life down-under? – Erich Mirabal Jul 29 '09 at 15:52