/ 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
2.
2. Step 2: include the CoreLocation.h file
3. implement the following codes
2.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
// view controller .h file #import <CoreLocation/CoreLocation.h> @interface StoreInputViewController : UIViewController <CLLocationManagerDelegate> { ... CLLocationManager *locationManager; } - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation; // view controller .m file // init the location manager in the viewDidLoad - (void)viewDidLoad { [super viewDidLoad]; // initialize the GPS locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self; locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m } // check whether GPS is enabled - (BOOL) isGPSEnabled { if (! ([CLLocationManager locationServicesEnabled]) || ( [CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied)) { return NO; } return YES; } // function to start capture GPS, we check settings first to see if GPS is disabled before attempting to get GPS - (void) getGPS { if([self isGPSEnabled]) { // Location Services is not disabled, get it now [locationManager startUpdatingLocation]; } else { // Location Services is disabled, do sth here to tell user to enable it } } // receiving the gps and stop it immediately (one time gps only for this example) - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { int degrees = newLocation.coordinate.latitude; int minutes = decimal * 60; double seconds = decimal * 3600 - minutes * 60; degrees, minutes, seconds]; degrees = newLocation.coordinate.longitude; minutes = decimal * 60; seconds = decimal * 3600 - minutes * 60; degrees, minutes, seconds]; NSLog(@"Lat: %@ Long:%@", latValue, longValue); // stop updating [manager stopUpdatingLocation]; }