views:

1891

answers:

9

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

+3  A: 

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
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
+3  A: 

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
There is a compass in the iPhone 3GS
Greg Beech
@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
@Greg Beech - Very true and I have updated my answer to reflect this. Such is the problem with ever-changing technology.
lc
@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
@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
A: 

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.

Ben Daniel
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.
Ben Daniel
@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
@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
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.
Ben Daniel
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
Ben Daniel
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
+6  A: 

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
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
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
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
A: 

Hi Anne, thank you, that's exactly what I was looking for though I suspect that under the hood it's doing what stalepretzel suggested.

Does anyone have any experience using CLLocation.course?

Rhett
The documentation seems to imply that it is only accurate if the device is moving. So I'd think that it does something similar to (or exactly) what stalepretzel suggested.
Ashley Clark
biocio: This answer should actually be a comment on Anne's answer. Stack Overflow is not a discussion forum.
Chris Hanson
A: 

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
A: 

If you've not seen the iPhone 3GS, this has a compass. I'm not certain if this is available to use in the 3.0 SDK though.

ing0
A: 

No iPhone can determine which direction I am facing. It can however (3GS) determine the approximate direction of North, South, etc.

Gerard
A: 

@Niels Hansen

Hi Hansen i have seen your answer. and i need solution for,finding the direction from current location location . But my requirement is i have to find direction from a state to all the temples in that state . if possible will you send the example code for me.

Thanks and Regards Sandhya

sandhya