Return to Snippet

Revision: 44541
at April 13, 2011 21:10 by magickaito


Updated Code
// 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;
    double decimal = fabs(newLocation.coordinate.latitude - degrees);
    int minutes = decimal * 60;
    double seconds = decimal * 3600 - minutes * 60;
    NSString *latValue = [NSString stringWithFormat:@"%d° %d' %1.4f\"", 
                     degrees, minutes, seconds];
    
    degrees = newLocation.coordinate.longitude;
    decimal = fabs(newLocation.coordinate.longitude - degrees);
    minutes = decimal * 60;
    seconds = decimal * 3600 - minutes * 60;
    NSString *longValue = [NSString stringWithFormat:@"%d° %d' %1.4f\"", 
                       degrees, minutes, seconds];
    
    NSLog(@"Lat: %@ Long:%@", latValue, longValue);
    
    // stop updating
    [manager stopUpdatingLocation];
}

Revision: 44540
at April 13, 2011 20:48 by magickaito


Initial Code
// view controller .h file

@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;
    double decimal = fabs(newLocation.coordinate.latitude - degrees);
    int minutes = decimal * 60;
    double seconds = decimal * 3600 - minutes * 60;
    NSString *latValue = [NSString stringWithFormat:@"%d° %d' %1.4f\"", 
                     degrees, minutes, seconds];
    
    degrees = newLocation.coordinate.longitude;
    decimal = fabs(newLocation.coordinate.longitude - degrees);
    minutes = decimal * 60;
    seconds = decimal * 3600 - minutes * 60;
    NSString *longValue = [NSString stringWithFormat:@"%d° %d' %1.4f\"", 
                       degrees, minutes, seconds];
    
    NSLog(@"Lat: %@ Long:%@", latValue, longValue);
    
    // stop updating
    [manager stopUpdatingLocation];
}

Initial URL


Initial Description
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.

Initial Title
Get GPS From Ipad using CLLocationManager

Initial Tags


Initial Language
Objective C