Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning iOS5 Development.pdf
Скачиваний:
7
Добавлен:
09.05.2015
Размер:
15.6 Mб
Скачать

644

CHAPTER 18: Where Am I? Finding Your Way with Core Location

newLocation.altitude]; altitudeLabel.text = altitudeString;

NSString *verticalAccuracyString = [NSString stringWithFormat:@"%gm", newLocation.verticalAccuracy];

verticalAccuracyLabel.text = verticalAccuracyString;

NOTE: Both the longitude and latitude are displayed in formatting strings containing the crypticlooking \u00B0. This represents the Unicode representation of the degree symbol (°). It’s never a good idea to put anything other than ASCII characters directly in a source code file, but including the hex value in a string is just fine, and that’s what we’ve done here.

Determining Distance Traveled

Finally, we determine the distance between the current location and the location stored in startingPoint and display the distance. While this application runs, if the user moves far enough for the location manager to detect the change, the Distance Traveled: field will be continually updated with the distance away from where the user was when the application was started.

CLLocationDistance distance = [newLocation distanceFromLocation:startingPoint];

NSString *distanceString = [NSString stringWithFormat:@"%gm", distance]; distanceTraveledLabel.text = distanceString;

And there you have it. Core Location is fairly straightforward and easy to use.

Before you can compile this program, you need to add CoreLocation.framework to your project. You do this the same as you did in Chapter 7 when you added

AudioToolbox.framework, except you choose CoreLocation.framework instead of

AudioToolbox.framework. Here’s a hint: click the first line in the project navigator (the blue WhereAmI icon), click the WhereAmI icon under TARGETS, and then click the Build Phases tab. Expand the Link Binary With Libraries disclosure triangle, and add your framework.

Compile and run the application, and try it. If you have the ability to run the application on your iPhone or iPad, try going for a drive with the application running and watch the values change as you drive. Um, actually, it’s better to have someone else do the driving!

Wherever You Go, There You Are

You’ve now seen pretty much all there is to Core Location. Although the underlying technology is quite complex, Apple has provided a simple interface that hides most of the complexity, making it quite easy to add location-related features to your applications so you can tell where the users are and identify when they move.

And speaking of moving, when you’re ready, proceed directly to the next chapter so we can play with the iPhone’s built-in accelerometer.

www.it-ebooks.info

Chapter19

Whee! Gyro and

Accelerometer!

One of the coolest features of the iPhone, iPad, and iPod touch is the built-in accelerometer—the tiny device that lets iOS know how the device is being held and if it’s being moved. iOS uses the accelerometer to handle autorotation, and many games use it as a control mechanism. The accelerometer can also be used to detect shakes and other sudden movement. This capability was extended even further with the introduction of the iPhone 4, which also includes a built-in gyroscope that lets you determine the angle at which the device is positioned around each axis. The gyro and accelerometer are now standard fare on all new iPads and iPod touches. In this chapter, we’re going to introduce you to the use of the Core Motion framework to access the gyro and accelerometer values in your application.

Accelerometer Physics

An accelerometer measures both acceleration and gravity by sensing the amount of inertial force in a given direction. The accelerometer inside your iOS device is a threeaxis accelerometer, meaning that it is capable of detecting either movement or the pull of gravity in three-dimensional space. This means that you can use the accelerometer to discover not only how the device is currently being held (as autorotation does), but also if it’s laying on a table, and even whether it’s face down or face up.

Accelerometers give measurements in g-forces (g for gravity), so a value of 1.0 returned by the accelerometer means that 1 g is sensed in a particular direction, as in these examples:

If the device is being held still with no movement, there will be approximately 1 g of force exerted on it by the pull of the earth.

If the device is being held perfectly upright, in portrait orientation, it will detect and report about 1 g of force exerted on its y axis.

D. Mark et al., Beginning iOS 5 Development

© Dave Mark, Jack Nutting, Jeff LaMarche 2011

www.it-ebooks.info

646CHAPTER 19: Whee! Gyro and Accelerometer!

If the device is being held at an angle, that 1 g of force will be distributed along different axes depending on how it is being held. When held at a 45-degree angle, the 1 g of force will be split roughly equally between two of the axes.

Sudden movement can be detected by looking for accelerometer values considerably larger than 1 g. In normal usage, the accelerometer does not detect significantly more than 1 g on any axis. If you shake, drop, or throw your device, the accelerometer will detect a greater amount of force on one or more axes. (Please do not drop or throw your own iOS device to test this theory.)

Figure 19–1 shows a graphic representation of the three axes used by the accelerometer. Notice that the accelerometer uses the more standard convention for the y coordinate, with increases in y indicating upward force, which is the opposite of Quartz 2D’s coordinate system (discussed in Chapter 16). When you are using the accelerometer as a control mechanism with Quartz 2D, you need to translate the y coordinate. When working with OpenGL ES, which is more likely when you are using the accelerometer to control animation, no translation is required.

Figure 19–1. The iPhone accelerometer’s axes in three dimensions. The front view of an iPhone 4 on the left shows the x and y axes. The side view of an iPhone 4 on the right shows the z axis.

Don’t Forget Rotation

We mentioned earlier that iPhone 4 also includes a gyroscope sensor that lets you read values describing the device’s rotation around its axes.

If the difference between the gyroscope and the accelerometer seems unclear, consider an iPhone lying flat on a table. If you begin to turn the phone around while it’s lying flat,

www.it-ebooks.info

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]