Posted By

espinallab on 02/08/10


Tagged

CLLocationManager


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

umang_nine
bobbym245
Tintenklecks


Distance Travel


 / Published in: Objective C
 

URL: http://github.com/beepscore/Distance/tree/master/Classes/

  1. //
  2. // DistanceViewController.h
  3. // Distance
  4. //
  5. // Created by Steve Baker on 1/30/10.
  6. // Copyright Beepscore LLC 2010. All rights reserved.
  7. //
  8.  
  9. #import <UIKit/UIKit.h>
  10. #import <CoreLocation/CoreLocation.h>
  11.  
  12. // Note we only write to the textFields, not edit, respond to keyboard input or read from them.
  13. // So we don't need to conform to or implememnt <UITextFieldDelegate> protocol
  14. // Ref Dudney sec 4.6 pg 65-66
  15. @interface DistanceViewController : UIViewController <CLLocationManagerDelegate> {
  16.  
  17. CLLocationManager *locationManager;
  18. NSMutableArray *locations;
  19. UILabel *lastUpdate;
  20. UILabel *totalDistance;
  21. UILabel *averageSpeed;
  22.  
  23. }
  24. #pragma mark -
  25. #pragma mark properties
  26.  
  27. @property(nonatomic,retain)CLLocationManager *locationManager;
  28. @property(nonatomic,retain)NSMutableArray *locations;
  29. @property(nonatomic,retain)IBOutlet UILabel *lastUpdate;
  30. @property(nonatomic,retain)IBOutlet UILabel *totalDistance;
  31. @property(nonatomic,retain)IBOutlet UILabel *averageSpeed;
  32.  
  33. - (void)updateDisplay;
  34.  
  35. @end
  36.  
  37.  
  38.  
  39.  
  40. //
  41. // DistanceViewController.m
  42. // Distance
  43. //
  44. // Created by Steve Baker on 1/30/10.
  45. // Copyright Beepscore LLC 2010. All rights reserved.
  46. //
  47.  
  48. #import "DistanceViewController.h"
  49.  
  50. @implementation DistanceViewController
  51.  
  52.  
  53. #pragma mark -
  54. #pragma mark properties
  55.  
  56. @synthesize locationManager;
  57. @synthesize locations;
  58. @synthesize lastUpdate;
  59. @synthesize totalDistance;
  60. @synthesize averageSpeed;
  61.  
  62. #pragma mark -
  63. #pragma mark initializers
  64. /*
  65. // The designated initializer. Override to perform setup that is required before the view is loaded.
  66. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  67. if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
  68. // Custom initialization
  69. }
  70. return self;
  71. }
  72. */
  73.  
  74. /*
  75. // Implement loadView to create a view hierarchy programmatically, without using a nib.
  76. - (void)loadView {
  77. }
  78. */
  79.  
  80.  
  81. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
  82. - (void)viewDidLoad {
  83. [super viewDidLoad];
  84. self.locationManager = [[[CLLocationManager alloc] init] autorelease];
  85. self.locationManager.delegate = self;
  86. // notify us only if distance changes by 10 meters or more
  87. self.locationManager.distanceFilter = 10.0f;
  88. self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
  89. [self.locationManager startUpdatingLocation];
  90. // 32 is a guess of a 'good' number
  91. self.locations = [NSMutableArray arrayWithCapacity:32];
  92. }
  93.  
  94.  
  95. /*
  96. // Override to allow orientations other than the default portrait orientation.
  97. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  98. // Return YES for supported orientations
  99. return (interfaceOrientation == UIInterfaceOrientationPortrait);
  100. }
  101. */
  102.  
  103.  
  104. #pragma mark destructors and memory cleanUp
  105. // use cleanUp method to avoid repeating code in setView, viewDidUnload, and dealloc
  106. - (void)cleanUp {
  107. [locationManager release], locationManager = nil;
  108. [locations release], locations = nil;
  109. [lastUpdate release], lastUpdate = nil;
  110. [totalDistance release], totalDistance = nil;
  111. [averageSpeed release], averageSpeed = nil;
  112. }
  113.  
  114.  
  115. // Release IBOutlets in setView.
  116. // Ref http://developer.apple.com/iPhone/library/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmNibObjects.html
  117. //
  118. // http://moodle.extn.washington.edu/mod/forum/discuss.php?d=3162
  119. - (void)setView:(UIView *)aView {
  120.  
  121. if (!aView) { // view is being set to nil
  122. // set outlets to nil, e.g.
  123. // self.anOutlet = nil;
  124. [self cleanUp];
  125. }
  126. // Invoke super's implementation last
  127. [super setView:aView];
  128. }
  129.  
  130.  
  131. - (void)didReceiveMemoryWarning {
  132. // Releases the view if it doesn't have a superview.
  133. [super didReceiveMemoryWarning];
  134.  
  135. // Release any cached data, images, etc that aren't in use.
  136. }
  137.  
  138.  
  139. - (void)viewDidUnload {
  140. // Release any retained subviews of the main view.
  141. [self cleanUp];
  142. }
  143.  
  144.  
  145.  
  146. - (void)dealloc {
  147. [self cleanUp];
  148. [super dealloc];
  149. }
  150.  
  151. #pragma mark Location methods
  152. - (void)locationManager:(CLLocationManager *)manager
  153. didUpdateToLocation:(CLLocation *)newLocation
  154. fromLocation:(CLLocation *)oldLocation {
  155. // if we have a valid location, and it's within 20 meters then stop
  156. // updating location, but turn it back on in 60 seconds.
  157. if (newLocation.horizontalAccuracy > 0.0f
  158. && newLocation.horizontalAccuracy < 20.0f) {
  159. if (self.locations.count > 3) {
  160. [self.locationManager stopUpdatingLocation];
  161. [self.locationManager performSelector:@selector(startUpdatingLocation)
  162. withObject:nil
  163. afterDelay:60.0f];
  164. }
  165. [self.locations addObject:newLocation];
  166. [self updateDisplay];
  167. }
  168. }
  169.  
  170. - (CLLocationDistance)totalDistanceTraveled {
  171. CGFloat totalDistanceTraveled = 0.0f;
  172. CLLocation *oldLocation = nil;
  173. for (CLLocation *location in self.locations) {
  174. if (nil == oldLocation) {
  175. oldLocation = location;
  176. // continue skips the rest of the for loop
  177. continue;
  178. }
  179. totalDistanceTraveled += fabs([location getDistanceFrom:oldLocation]);
  180. oldLocation = location;
  181. }
  182. return totalDistanceTraveled;
  183. }
  184.  
  185. - (NSTimeInterval)timeDelta {
  186. NSDate *first = [(CLLocation *)[self.locations objectAtIndex:0] timestamp];
  187. NSDate *last = [(CLLocation *)[self.locations lastObject] timestamp];
  188. return [last timeIntervalSince1970] - [first timeIntervalSince1970];
  189. }
  190.  
  191.  
  192. #pragma mark -
  193. - (void)updateDisplay {
  194. CLLocationDistance distance = [self totalDistanceTraveled];
  195. totalDistance.text = [NSString stringWithFormat:@"%5.3f", distance];
  196.  
  197. NSTimeInterval time = [self timeDelta];
  198. // don't want to divide by zero
  199. if (0.0f == time) {
  200. averageSpeed.text = @"0.000";
  201. } else {
  202. averageSpeed.text = [NSString stringWithFormat:@"%5.3f", distance/time];
  203. }
  204. NSDateFormatter *inputFormatter = [[[NSDateFormatter alloc] init] autorelease];
  205. [inputFormatter setDateFormat:@"HH:mm:ss.SSSS"];
  206. NSDate *date = [(CLLocation *)[self.locations lastObject] timestamp];
  207. lastUpdate.text = [inputFormatter stringFromDate:date];
  208. }
  209.  
  210. @end

Report this snippet  

You need to login to post a comment.