Apple/iOS/Kits/CoreLocation/iOS6

Un article de Agora2ia.


See Location Awareness Programming Guide

Classes impliquées dans le framework CoreLocation:

  • CLLocationManager
  • CLHeading
    • Accessing the Heading Attributes
      • magneticHeading property
      • trueHeading property
      • headingAccuracy property
      • timestamp property
      • - description
    • Accessing the Raw Heading Data
      • x property
      • y property
      • z property


Il y a une différence entre :


Exemple

Sample: Teslameter (from http://developer.apple.com)

#pragma mark - Private

- (void)startHeadingEvents {
	// Instanciate Location Manager.
	CLLocationManager* locationManager = [[CLLocationManager alloc] init];
	locationManager.delegate = self; // CLLocationManagerDelegate

	// Determine whether heading events are available
	if (locationManager.headingAvailable) {

		// Start location services to get the true heading.
		locationManager.distanceFilter = 1000;
		locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
		[locationManager startUpdatingLocation];

		// Start heading updates.
		locationManager.headingFilter = 5;
		[locationManager startUpdatingHeading];

		// see in CLLocationManager Class Reference
		// locationManager.headingOrientation =
	}
}

#pragma mark - Lifecycle

- (void) viewDidLoad {
	[super viewDidLoad];
	
	[self startHeadingEvents];
}

- (void) viewDidUnload {
	[locationManager stopUpdatingHeading];
	[locationManager stopUpdatingLocation];
	
	[super viewDidUnload];
}

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {

	// ensure that the data you just received is valid
	if (newHeading.headingAccuracy < 0) return;
 
   // Use the true heading if it is valid.
	CLLocationDirection  headingInDegrees = ((newHeading.trueHeading > 0) ?
			newHeading.trueHeading : newHeading.magneticHeading);
 
	// magneticHeading & trueHeading
	//
	// In iOS 3.x and earlier, the value in this property is always measured relative to the top 
	// of the device in a portrait orientation, regardless of the device’s actual physical or 
	// interface orientation. In iOS 4.0 and later, the value is measured relative to the heading 
	// orientation specified by the location manager. For more information, see the 
	// headingOrientation property in CLLocationManager Class Reference.

	// self.currentHeading = headingInDegrees;
	// [self updateHeadingDisplays];
	// float headingInRadians = (heading * M_PI / 180); // assuming needle points to top of iphone.
	// self.bearingView.transform = CGAffineTransformMakeRotation(headingDegrees);
	self.headingLabel.text = [NSString stringWithFormat:@"heading=%d\nx=%f - y=%f - z=%f", 
			(int)headingInRadians, newHeading.x, newHeading.y, newHeading.z];
}