Monday, October 10, 2011

Get Location Data (GPS) in iPhone

Using this post you can learn about 
  • How to create shared object in Obective C
  • How to create custom 'init' method
  • How to get GPS location using iPhone 

//Reciving data for main screen
    POLocationService* locationService = [POLocationService sharedLocationService];
    locationService.delegate = self;
    [locationService requestLocationInformationFor:@"for finding deals near to you!"];*/


 POLocationService.h   file


#import "Foundation/Foundation.h"
#import "CoreLocation/CoreLocation.h"

@interface POLocationService : NSObject "CLLocationManagerDelegate"{
    CLLocationManager *locationManager_;
    id delegate_;
    NSString* locationString_;
}

@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, assign) id delegate;
@property (nonatomic, retain) id locationString;

+ (POLocationService *)sharedLocationService;
- (void)requestLocationInformationFor:(NSString *)purpose;
 
@end



 POLocationService.m   file


#import "POLocationService.h"

static POLocationService *sharedLocationService = nil;


@implementation POLocationService

@synthesize locationManager = locationManager_;
@synthesize delegate = delegate_;
@synthesize locationString = locationString_;

+ (POLocationService *)sharedLocationService
{
    @synchronized(self) {
        if (sharedLocationService == nil) {
            sharedLocationService = [[self alloc] init];
        }
    }
    return sharedLocationService;
}

- (id)init
{
    if (self = [super init]) {
        self.locationManager = [[CLLocationManager alloc] init];
    }
   
    return self;
}

- (void)requestLocationInformationFor:(NSString *)purpose
{
    //didRequest_ = YES;
    locationManager_.delegate = self;
    locationManager_.purpose = purpose;
    locationManager_.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    locationManager_.distanceFilter = 200;
    [locationManager_ startUpdatingLocation];
}

/*
 * We want to get and store a location measurement that meets the desired accuracy. For this example, we are
 *      going to use horizontal accuracy as the deciding factor. In other cases, you may wish to use vertical
 *      accuracy, or both together.
 */
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    NSString *location = [NSString stringWithFormat:@"%lf;%lf",newLocation.coordinate.latitude,newLocation.coordinate.longitude];

    self.locationString = location;
   
    [delegate_ performSelector:@selector(didFindLocation:userDenied:) withObject:location withObject:[NSNumber numberWithInt:0]]; // Third arg is whether user denied
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    // The location "unknown" error simply means the manager is currently unable to get the location.
    if ([error code] != kCLErrorLocationUnknown) {
        [locationManager_ stopUpdatingLocation];
        locationManager_.delegate = nil;
        [delegate_ performSelector:@selector(didFindLocation:userDenied:) withObject:nil withObject:[NSNumber numberWithInt:0]];
    } else if ([error code] != kCLErrorDenied || [error code] != kCLErrorRegionMonitoringDenied) {
        [delegate_ performSelector:@selector(didFindLocation:userDenied:) withObject:nil withObject:[NSNumber numberWithInt:0]];
    } else {
        [delegate_ performSelector:@selector(didFindLocation:userDenied:) withObject:nil withObject:[NSNumber numberWithInt:0]];
    }

}

- (void)dealloc
{
    [locationManager_ stopUpdatingLocation];
    locationManager_.delegate = nil;
    [locationManager_ release];
    [super dealloc];
}
@end