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

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

635

not to notify you of every change, and instead to notify you only when the location changes by more than a certain amount. Setting up a distance filter can reduce the amount of polling your application does.

Distance filters are also set in meters. Specifying a distance filter of 1000 tells the location manager not to notify its delegate until the iPhone has moved at least 1,000 meters from its previously reported position. Here’s an example:

locationManager.distanceFilter = 1000.0f;

If you ever want to return the location manager to the default setting of no filter, you can use the constant kCLDistanceFilterNone, like this:

locationManager.distanceFilter = kCLDistanceFilterNone;

Just as when specifying the desired accuracy, you should take care to avoid getting updates any more frequently than you really need them, because otherwise you waste battery power. A speedometer app that’s calculating the user’s velocity based on the user’s location will probably want to have updates as fast as possible, but an app that’s going to show the nearest fast-food restaurant can get by with a lot less.

Starting the Location Manager

When you’re ready to start polling for location, you tell the location manager to start. It will go off and do its thing, and then call a delegate method when it has determined the current location. Until you tell it to stop, it will continue to call your delegate method whenever it senses a change that exceeds the current distance filter.

Here’s how you start the location manager:

[locationManager startUpdatingLocation];

Using the Location Manager Wisely

If you need to determine the current location only and have no need to continuously poll for location, you should have your location delegate stop the location manager as soon as it gets the information your application requires. If you need to continuously poll, make sure you stop polling as soon as you possibly can. Remember that as long as you are getting updates from the location manager, you are putting a strain on the user’s battery.

To tell the location manager to stop sending updates to its delegate, call stopUpdatingLocation, like this:

[locationManager stopUpdatingLocation];

The Location Manager Delegate

The location manager delegate must conform to the CLLocationManagerDelegate protocol, which defines two methods, both of which are optional. One of these methods

www.it-ebooks.info

636

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

is called by the location manager when it has determined the current location or when it detects a change in location. The other method is called when the location manager encounters an error.

Getting Location Updates

When the location manager wants to inform its delegate of the current location, it calls the locationManager:didUpdateToLocation:fromLocation: method. This method takes three parameters:

The first parameter is the location manager that called the method.

The second parameter is a CLLocation object that defines the current location of the device.

The third parameter is a CLLocation object that defines the previous location from the last update.

The first time this method is called, the previous location object will be nil.

Getting Latitude and Longitude Using CLLocation

Location information is passed from the location manager using instances of the CLLocation class. This class has five properties that might be of interest to your application. The latitude and longitude are stored in a property called coordinate. To get the latitude and longitude in degrees, do this:

CLLocationDegrees latitude = theLocation.coordinate.latitude;

CLLocationDegrees longitude = theLocation.coordinate.longitude;

The CLLocation object can also tell you how confident the location manager is in its latitude and longitude calculations. The horizontalAccuracy property describes the radius of a circle with the coordinate as its center. The larger the value in horizontalAccuracy, the less certain Core Location is of the location. A very small radius indicates a high level of confidence in the determined location.

You can see a graphic representation of horizontalAccuracy in the Maps application (see Figure 18–1). The circle shown in Maps uses horizontalAccuracy for its radius when it detects your location. The location manager thinks you are at the center of that circle. If you’re not, you’re almost certainly somewhere inside the circle. A negative value in horizontalAccuracy is an indication that you cannot rely on the values in coordinate for some reason.

www.it-ebooks.info

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

637

Figure 18–1. The Maps application uses Core Location to determine your current location. The outer circle is a visual representation of the horizontal accuracy.

The CLLocation object also has a property called altitude that can tell you how many meters above or below sea level you are:

CLLocationDistance altitude = theLocation.altitude;

Each CLLocation object maintains a property called verticalAccuracy that is an indication of how confident Core Location is in its determination of altitude. The value in altitude could be off by as many meters as the value in verticalAccuracy. If the verticalAccuracy value is negative, Core Location is telling you it could not determine a valid altitude.

CLLocation objects also have a timestamp that tells when the location manager made the location determination.

In addition to these properties, CLLocation has a useful instance method that will let you determine the distance between two CLLocation objects. The method is called distanceFromLocation:, and it works like this:

CLLocationDistance distance = [fromLocation distanceFromLocation:toLocation];

The preceding line of code will return the distance between two CLLocation objects, fromLocation and toLocation. This distance value returned will be the result of a greatcircle distance calculation that ignores the altitude property and calculates the

www.it-ebooks.info

638

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

distance as if both points were at sea level. For most purposes, a great-circle calculation will be more than sufficient, but if you do want to take altitude into account when calculating distances, you’ll need to write your own code to do it.

NOTE: If you’re not sure what’s meant by great-circle distance, you might want to think back to geography class and the notion of a great-circle route. The idea is that the shortest distance between any two points on the earth’s surface will be found along a route that goes the entire way around the earth: a “great circle.” The calculation performed by CLLocation determines the distance between two points along such a route, taking the curvature of the earth into account. Without accounting for that curvature, you would end up with the length of a straight line connecting the two points, which isn’t much use, since that line would invariably go straight through some amount of the earth itself!

Error Notifications

If Core Location is not able to determine your current location, it will call a second delegate method named locationManager:didFailWithError:. The most likely cause of an error is that the user denies access. The user must authorize use of the location manager, so the first time your application wants to determine the location, an alert will pop up on the screen asking if it’s OK for the current program to access your location (see Figure 18–2).

www.it-ebooks.info

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