How to build a compass with the iPhone?

点点圈 提交于 2019-12-04 07:17:50

Yes, you are right, that's the only way. Although I think once you know the direction, you could track small movements of the phone from acceleration sensors and draw the direction accurately even if the original movement stops.

Note: This is outdated info, it was correct up until 3Gs came out. Unfortunately I can't delete accepted answer so just look below.

With the original iPhone and iPhone 3G, no.

With the iPhone 3GS available June 2009, yes. It includes a magnetometer which would allow for a compass application.

The Core Location Framework provides this information in the CLHeading class. You can check if the device supports this function with the magnetometer key.

As others have said, with the iPhone 3G you would be able to tell the direction of movement when the phone is moving, but you would not be able to tell the orientation of the phone to indicate direction to the user. Certainly the user can be instructed to orient the phone in a certain manner which combined with the movement could indicate direction accurately.

As an update to this, the iPhone 3GS does include an actual, working digital compass that is available to apps. It makes writing such an app trivial.

See the CLHeading class and the magneticHeading property.

http://developer.apple.com/iphone/library/documentation/CoreLocation/Reference/CLHeading_Class/Reference/Reference.html

You can also get a true north heading, assuming that the phone has had time to locate itself geographically.

By scanning the available applications I've come to the conclusion that there are two ways to make a compass for the current iPhone 3G.

  1. Require the phone to move. Use the difference in GPS data to determine the direction.
  2. Require the user to point the phone at the sun. Use GPS and time information to determine directions.

You just need to implement location manager functionality to find the exact direction in which your iPhone is pointing.

So, You can check this :

CLLocationManager *locationManager; [locationManager startUpdatingHeading];

And whenever you call startUpdatingHeading() it will call "- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading" method which you need to override from Location manager.

And that's it you can find the direction in which iPhone 3GS is pointing by newHeading.magneticHeading or newHeading.trueHeading.

You will surprise that you can get exact the same direction what magnetic compass gives you.

The compass will only show the direction you're moving. It will not tell you the direction the iPhone is positioned in. So even using the accelerometer to track that the iPhone has turned/moved will not necessarily let you point to north on the display.

(Ie. you won't know if its moving sideways or if we're holding the iPhone upside-down or anything. You'll just know where you're moving...)

EDIT december 2010: Since this original post was written, the iPhone 3GS (launched summer '09) and newer models have an electronic compass. This compass can show the correct direction when without moving... (This means that the compass now is able to point to north regardless of the direction the iPhone is held or is moving.)

I think your right with your suspicion, a GPS position is a single point without any direction information. To determine a direction you need two points to create a direction vector. Car navigation systems work the same way. They can only show a direction vector if the car is moving.

If you implement an analogue clock, you can use the instructions below to superimpose a compass dial.

This could be used to 'calibrate' the compass and use the accelerometer remain pointing to North when the phone moves.

Obviously this is not super accurate... "low tech solution to a high tech problem"

(instruction taken from http://www.qwerty.co.za)

In the northern hemisphere place the watch on a level piece of ground, point the hour hand in the direction of the sun. The North-South line is again the line dividing the angle between the hour hand and the 12 o'clock line in half.

In the Southern hemisphere, place the watch on a level piece of ground, point the figure 12 towards the sun. The North-South line is the line dividing the angle between the hour hand and the 12 o'clock line in half.

As a side note, the accelerometers in the iphone don't track rotations only translations so you can't do proper 6 axis inertial tracking of position and direction.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!