WhereAmIViewController.m
上传用户:jjjjag8
上传日期:2017-04-17
资源大小:1443k
文件大小:3k
源码类别:

iPhone

开发平台:

MultiPlatform

  1. //
  2. //  WhereAmIAppDelegate.m
  3. //  WhereAmI
  4. //
  5. //  Created by Jeff LaMarche on 8/4/08.
  6. //  Copyright __MyCompanyName__ 2008. All rights reserved.
  7. //
  8. #import "WhereAmIViewController.h"
  9. @implementation WhereAmIViewController
  10. @synthesize locationManager;
  11. @synthesize startingPoint;
  12. @synthesize latitudeLabel;
  13. @synthesize longitudeLabel;
  14. @synthesize horizontalAccuracyLabel;
  15. @synthesize altitudeLabel;
  16. @synthesize verticalAccuracyLabel;
  17. @synthesize distanceTraveled;
  18. #pragma mark -
  19. - (void)viewDidLoad {
  20. self.locationManager = [[CLLocationManager alloc] init];
  21. [locationManager startUpdatingLocation];
  22. locationManager.delegate = self;
  23. locationManager.distanceFilter = kCLDistanceFilterNone; 
  24. locationManager.desiredAccuracy = kCLLocationAccuracyBest;
  25. }
  26. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  27. // Return YES for supported orientations
  28. return (interfaceOrientation == UIInterfaceOrientationPortrait);
  29. }
  30. - (void)didReceiveMemoryWarning {
  31. [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
  32. // Release anything that's not essential, such as cached data
  33. }
  34. - (void)dealloc {
  35. [locationManager release];
  36. [startingPoint release];
  37. [latitudeLabel release];
  38. [longitudeLabel release];
  39. [horizontalAccuracyLabel release];
  40. [altitudeLabel release];
  41. [verticalAccuracyLabel release];
  42. [distanceTraveled release];
  43. [super dealloc];
  44. }
  45. #pragma mark -
  46. #pragma mark CLLocationManagerDelegate Methods
  47. - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
  48. if (startingPoint == nil)
  49. self.startingPoint = newLocation;
  50. NSString *latitudeString = [[NSString alloc] initWithFormat:@"%g°", newLocation.coordinate.latitude];
  51. latitudeLabel.text = latitudeString;
  52. [latitudeString release];
  53. NSString *longitudeString = [[NSString alloc] initWithFormat:@"%g°", newLocation.coordinate.longitude];
  54. longitudeLabel.text = longitudeString;
  55. [longitudeString release];
  56. NSString *horizontalAccuracyString = [[NSString alloc] initWithFormat:@"%gm", newLocation.horizontalAccuracy];
  57. horizontalAccuracyLabel.text = horizontalAccuracyString;
  58. [horizontalAccuracyString release];
  59. NSString *altitudeString = [[NSString alloc] initWithFormat:@"%gm", newLocation.altitude];
  60. altitudeLabel.text = altitudeString;
  61. [altitudeString release];
  62. NSString *verticalAccuracyString = [[NSString alloc] initWithFormat:@"%gm", newLocation.verticalAccuracy];
  63. verticalAccuracyLabel.text = verticalAccuracyString;
  64. [verticalAccuracyString release];
  65. CLLocationDistance distance = [newLocation getDistanceFrom:startingPoint];
  66. NSString *distanceString = [[NSString alloc] initWithFormat:@"%gm", distance];
  67. distanceTraveled.text = distanceString;
  68. [distanceString release];
  69. }
  70. - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
  71. NSString *errorType = (error.code == kCLErrorDenied) ? @"Access Denied" : @"Unknown Error";
  72. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error gettingg location from Core Location" message:errorType delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
  73. [alert show];
  74. [alert release];
  75. }
  76. @end