WHAT YOU WILL LEARN IN THIS CHAPTER
How to obtain accelerometer data from your iPhone or iPod Touch
How to detect shakes to your device
How to obtain geographical data using the Core Location service in the iPhone and iPod Touch
How to display a map in your application
In the previous chapter, you saw how you could access the built-in applications on an iPhone and iPod Touch through various means — URL strings as well as using the specialized classes provided by the iPhone SDK. In this chapter, you learn how to access the hardware of your device, such as the accelerometer, and obtain location information through GPS, cell towers, and wireless hotspots.
One of the most innovative features of the iPhone and iPod Touch is the built-in accelerometer. The accelerometer allows the device to detect the orientation of the device and adapts the content to suit the new orientation. For example, when you rotate your device sideways, the Safari Web browser automatically switches the screen to landscape mode so that you now have a wider viewing space. Similarly, the camera relies on the accelerometer to tell it whether you are taking a picture in portrait or landscape mode.
The accelerometer in iPhone and iPod Touch measures the acceleration of the device relative to freefall. A value of 1 indicates that the device is experiencing 1 g of force exerting on it (1 g of force being the gravitational pull of the earth, which your device experiences when it is stationary). The accelerometer measures the acceleration of the device in three different axes: X, Y, and Z. Figure 17-1 shows the different axes measured by the accelerometer.
Table 17-1 shows the various readings of the three axes when the device is in the various positions.
Table 17.1. The Various Readings of the X, Y, and Z Axes
Position | X | Y | Z |
---|---|---|---|
Vertical upright position | 0.0 | −1.0 | 0.0 |
Landscape Left | 1.0 | 0.0 | 0.0 |
Landscape Right | −1.0 | 0.0 | 0.0 |
Upside Down | 0.0 | 1 | 0.0 |
Flat Up | 0.0 | 0.0 | −1.0 |
Flat Down | 0.0 | 0.0 | 1.0 |
If the device is held upright and moved to the right quickly, the value of the X-axis will increase from 0 to a positive value. If it is moved to the left quickly, the value of the X-axis will decrease from 0 to a negative value. If the device is moved upward quickly, the value of the Y-axis will increase from −1.0 to a larger value. If the device is moved download quickly, the value of the Y-axis will decrease from −1.0 to a smaller value.
If the device is lying flat on a table and then dropped, the value of the Z-axis will decrease from −1.0 to a smaller number. If it is moved upward, the value of the Z-axis will increase from −1.0 to a bigger number.
The accelerometer used on the iPhone and iPod Touch gives a maximum reading of about +/− 2.3G with a resolution of about 0.018 g.
In the following Try it Out, you learn how to programmatically access the data returned by the accelerometer. Obtaining the accelerometer data allows you to build very interesting applications, such as a spirit level, as well as games that depend on motion detection.
In iPhone OS 3.0, Apple provides the Shake API (discussed in the next section), which is a set of methods that allows you to detect shakes to the device. However, how do you detect shakes in iPhone OS 2.0 and earlier?
The answer is actually very simple. You can add some code in the accelerometer:didAccelerate:
event, like this:
#import "AccelerometerViewController.h" #define kAccelerationThreshold 2.2 //... //... - (void)accelerometer:(UIAccelerometer *) acc didAccelerate:(UIAcceleration *)acceleration {if (fabsf(acceleration.x) > kAccelerationThreshold)
{
NSLog(@"Shake detected");
}
}
The fabsf()
function returns the absolute value of a floating-point number. In this case, if the X-axis value registers an absolute value of more than 2.2, it is deemed that the user is shaking the device.
In iPhone OS 3.0, Apple announced the availability of a new Shake API that helps you to detect shakes to the device. In reality, this new Shake API comes in the form of three events that you can handle in your code:
motionBegan:
motionEnded:
motionCancelled:
These three events are defined in the UIResponder
class, which is the super class of UIApplication, UIView
, and its subclasses (including UIWindow
). The following Try It Out shows you how to detect shakes to your device using these three events.
Now that you know how to detect shaking on your devices, you can put it to good use. Using the same project, modify it so that when the device is shaken, you reset the DatePicker view to today's date.
Nowadays, mobile devices are commonly equipped with GPS receivers. Using a GPS receiver, you find your location easily because of the many satellites orbiting the earth, courtesy of the U.S. government. However, GPS requires a clear sky to work and hence does not work indoors. Also, the first-generation iPhone is not equipped with a GPS receiver.
Besides GPS, another effective way to locate one's position is through cell tower triangulation. When a mobile phone is switched on, it is constantly in contact with base stations surrounding it. By knowing the identity of cell towers, it is possible to correlate this information into a physical location through the use of various databases containing the cell towers" identity and their exact geographical location. Cell tower triangulation has its advantages over GPS because it works indoors, without the need to obtain information from satellites. However, it is not as accurate as GPS because its accuracy depends on the area you are in. Cell tower triangulation works best in densely populated areas where the cell towers are closely located. However, cell tower triangulation is not applicable to iPod Touch because it does not have a cellular phone in it.
The third method of locating one's position is to rely on Wi-Fi triangulation. Rather than connect to cell towers, the device connects to a Wi-Fi network and checks the service provider against databases to determine the location serviced by the provider. Of the three methods described here, Wi-Fi triangulation is the least accurate.
On the iPhone, Apple provides the Core Location framework to help you determine your physical location. The beauty of this framework is that it makes use of all three approaches mentioned, and whichever method it uses is totally transparent to the developer. You simply specify the accuracy you need, and Core Location determines the best way to obtain the results for you.
Sound amazing? It is. The following Try It Out shows you how this is done in code.
While obtaining the location value of a position is interesting, it is not of much use if you cannot visually locate it on a map. Hence, the most ideal situation would be to use the location information and display it on a map. Fortunately, iPhone SDK 3.0 comes with the Map Kit API to make displaying Google Maps in your application a snap. You see how this can be done in the following Try it Out.
In this chapter, you have seen how to manipulate the various types of hardware of your device: the accelerometer, Shake API, and location-based services using Core Location. Combining all this knowledge allows you to create very compelling applications.
EXERCISES
Name the protocol that your delegate needs to conform to in order to use the accelerometer on your iPhone and iPod Touch.
Name the three events in the Shake API in iPhone SDK 3.0.
Core Location uses three different methods to obtain a device's location. Discuss the various methods used by the iPhone and iPod Touch.
WHAT YOU HAVE LEARNED IN THIS CHAPTER
TOPIC | KEY CONCEPTS |
---|---|
Accessing the accelerometer | Ensure that your view controller conforms to the To listen to changes in acceleration, implement the |
Detecting Shakes | You can either use the accelerometer data or use the new Shake API in iPhone OS 3.0. For the Shake API, handle the following events: |
Obtaining location data | Add the Ensure that your view controller conforms to the To listen to changes in location, implement the |
Specifying accuracy for location data | Use one of the following constants:
|
Displaying Maps | Add the Create an instance of the |