Can the iPhone determine if you\'re facing north, south, east or west?
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!
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.
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.
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.
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.