Posted By

magickaito on 04/13/11


Tagged

objectivec


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Tintenklecks


Get GPS From Ipad using CLLocationManager


 / Published in: Objective C
 

  1. Step 1: add CoreLocation framework in Project Settings -> Build Phases -> Link Binary...
  2. Step 2: include the CoreLocation.h file
  3. implement the following codes
  1. // view controller .h file
  2.  
  3. #import <CoreLocation/CoreLocation.h>
  4. @interface StoreInputViewController : UIViewController <CLLocationManagerDelegate> {
  5. ...
  6. CLLocationManager *locationManager;
  7. }
  8.  
  9. - (void)locationManager:(CLLocationManager *)manager
  10. didUpdateToLocation:(CLLocation *)newLocation
  11. fromLocation:(CLLocation *)oldLocation;
  12.  
  13. // view controller .m file
  14.  
  15. // init the location manager in the viewDidLoad
  16. - (void)viewDidLoad
  17. {
  18. [super viewDidLoad];
  19.  
  20. // initialize the GPS
  21. locationManager = [[CLLocationManager alloc] init];
  22. locationManager.delegate = self;
  23. locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
  24. locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
  25. }
  26.  
  27. // check whether GPS is enabled
  28. - (BOOL) isGPSEnabled
  29. {
  30. if (! ([CLLocationManager locationServicesEnabled])
  31. || ( [CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied))
  32. {
  33. return NO;
  34. }
  35. return YES;
  36. }
  37.  
  38. // function to start capture GPS, we check settings first to see if GPS is disabled before attempting to get GPS
  39. - (void) getGPS
  40. {
  41. if([self isGPSEnabled])
  42. {
  43. // Location Services is not disabled, get it now
  44. [locationManager startUpdatingLocation];
  45. }
  46. else
  47. {
  48. // Location Services is disabled, do sth here to tell user to enable it
  49. }
  50. }
  51.  
  52. // receiving the gps and stop it immediately (one time gps only for this example)
  53. - (void)locationManager:(CLLocationManager *)manager
  54. didUpdateToLocation:(CLLocation *)newLocation
  55. fromLocation:(CLLocation *)oldLocation
  56. {
  57. int degrees = newLocation.coordinate.latitude;
  58. double decimal = fabs(newLocation.coordinate.latitude - degrees);
  59. int minutes = decimal * 60;
  60. double seconds = decimal * 3600 - minutes * 60;
  61. NSString *latValue = [NSString stringWithFormat:@"%d° %d' %1.4f\"",
  62. degrees, minutes, seconds];
  63.  
  64. degrees = newLocation.coordinate.longitude;
  65. decimal = fabs(newLocation.coordinate.longitude - degrees);
  66. minutes = decimal * 60;
  67. seconds = decimal * 3600 - minutes * 60;
  68. NSString *longValue = [NSString stringWithFormat:@"%d° %d' %1.4f\"",
  69. degrees, minutes, seconds];
  70.  
  71. NSLog(@"Lat: %@ Long:%@", latValue, longValue);
  72.  
  73. // stop updating
  74. [manager stopUpdatingLocation];
  75. }

Report this snippet  

You need to login to post a comment.