Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

6_Location

.pdf
Скачиваний:
3
Добавлен:
17.03.2016
Размер:
902.94 Кб
Скачать

Location Manager Accuracy and Distance

The receiver does its best to achieve the requested accuracy; however, the actual accuracy is not guaranteed.

locationManager.desiredAccuracy = kCLLocationAccuracyKilometer; extern const CLLocationAccuracy kCLLocationAccuracyBestForNavigation; Use the highest possible accuracy and combine it with additional sensor data. This level of accuracy is intended for use in navigation applications that require

precise position information at all times and are intended to be used only while the device is plugged in.

extern const CLLocationAccuracy kCLLocationAccuracyBest;

extern const CLLocationAccuracy kCLLocationAccuracyNearestTenMeters; extern const CLLocationAccuracy kCLLocationAccuracyHundredMeters; extern const CLLocationAccuracy kCLLocationAccuracyKilometer;

extern const CLLocationAccuracy kCLLocationAccuracyThreeKilometers;

The minimum distance (measured in meters) a device must move horizontally before an update event is generated.

locationManager.distanceFilter = 500; // meters

This distance is measured relative to the previously delivered location. Use the value kCLDistanceFilterNone to be notified of all movements. The default value.

Start the Location Updates

- (void)startStandardUpdates

{

//Create the location manager if this object does not

//already have one.

if (nil == locationManager)

locationManager = [[CLLocationManager alloc] init];

locationManager.delegate = self;// CLLocationManagerDelegate protocol locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;

// Set a movement threshold for new events. locationManager.distanceFilter = 500; // meters

[locationManager startUpdatingLocation];

}

[locationManager stopUpdatingLocation]; // To STOP

[locationManager requestLocation]; // For the one-time location delivery.

Start the Significant Location Updates

- (void)startSignificantChangeUpdates

{

//Create the location manager if this object does not

//already have one.

if (nil == locationManager)

locationManager = [[CLLocationManager alloc] init];

locationManager.delegate = self;

[locationManager startMonitoringSignificantLocationChanges];

}

Note: Significant-change location updates require an authorization status of kCLAuthorizationStatusAuthorizedAlways.

[locationManager stopMonitoringSignificantLocationChanges]; // To STOP

Receiving Location Data from a Service

CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {

// If it's a relatively recent event, turn off updates to save power. CLLocation* location = [locations lastObject];

// Because the location manager object sometimes returns cached events, it’s recommended that you check the timestamp of any location events you receive NSDate* eventDate = location.timestamp;

NSTimeInterval howRecent = [eventDate timeIntervalSinceNow]; if (abs(howRecent) < 15.0) {

// If the event is recent, do something with it. NSLog(@"latitude %+.6f, longitude %+.6f\n",

location.coordinate.latitude, location.coordinate.longitude);

}}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error;

CLLocation

Location Attributes coordinate

- latitude double CLLocationDegrees;

The latitude in degrees. Positive values indicate latitudes north of the equator. Negative values indicate latitudes south of the equator.

- longitude double CLLocationDegrees;

The longitude in degrees. Measurements are relative to the zero meridian, with positive values extending east of the meridian and negative values extending west of the meridian.

altitude double CLLocationDistance;

//Positive values indicate altitudes above sea level.floor verticalAccuracy // The accuracy of the altitude value in meters. timestamp // The time at which this location was determined description // The location data in a formatted text string: “<<latitude>,

<longitude>> +/- <accuracy>m (speed <speed> kph / heading <heading>) @ <date-time>

horizontalAccuracy // The location’s latitude and longitude identify the center of the circle, and this value indicates the radius of that circle. A negative value indicates that the location’s latitude and longitude are invalid.

Background Mode

@property(assign, nonatomic) BOOL allowsBackgroundLocationUpdates

Apps that want to receive location updates when suspended must include the

UIBackgroundModes key (with the location value) in their app’s Info.plist file and set the value of this property to YES. The presence of the UIBackgroundModes key with the location value is required for background updates; you use this property to enable and disable the behavior based on your app’s behavior. For example, you might set the value to YES only after the user enables features in your app where background updates are needed.

Deferring Location Updates While Your App Is in the

Background

It’s recommended that you use this feature when your app could process the location data later without any problems.

// Delegate method from the CLLocationManagerDelegate protocol. - (void)locationManager:(CLLocationManager *)manager

didUpdateLocations:(NSArray *)locations {

//Add the new locations to the hike [self.hike addLocations:locations];

//Defer updates until the user hikes a certain distance

//or when a certain amount of time has passed.

if (!self.deferringUpdates) {

CLLocationDistance distance = self.hike.goal - self.hike.distance; NSTimeInterval time = [self.nextAudible timeIntervalSinceNow];

[locationManager allowDeferredLocationUpdatesUntilTraveled:distance timeout:time];

self.deferringUpdates = YES;

}

}

Region Monitoring

CLCircularRegion

- (instancetype)initWithCenter:(CLLocationCoordinate2D)center radius:(CLLocationDistance)radius

identifier:(NSString *)identifier;

CLLocationManager

-(void)startMonitoringForRegion:(CLRegion *)region;

-(void)stopMonitoringForRegion:(CLRegion *)region;

Note: An app can register up to 20 regions at a time. In order to report region changes in a timely manner, the region monitoring service requires network connectivity.

Region Monitoring

@property(readonly, nonatomic, copy) NSSet <__kindof CLRegion *> *monitoredRegions

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region;

Region events are delivered to the locationManager:didEnterRegion: and locationManager:didExitRegion: methods of your delegate. If there is an error, the location manager calls the locationManager:monitoringDidFailForRegion:withError: method of your delegate instead.

Couple words about Beacon

To start beacon ranging, create a CLBeaconRegion object that identifies the beacons you want and pass that object to the startRangingBeaconsInRegion: method. As the location manager detects beacons, it delivers that information to its delegate’s locationManager:didRangeBeacons:inRegion: method.