Skip to content

Instantly share code, notes, and snippets.

@Duraiamuthan
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Duraiamuthan/11147471 to your computer and use it in GitHub Desktop.
Save Duraiamuthan/11147471 to your computer and use it in GitHub Desktop.
Objective C GeoLocations finder
//Header
#import <Foundation/Foundation.h>
#import "CoreLocation/CoreLocation.h"
@interface GeoLocation : NSObject<CLLocationManagerDelegate,UIAlertViewDelegate>
{
CLLocationManager *locationManager;
double latitude;
double longitude;
}
+ (id)sharedGeoLocationManager;
-(void)StartGpsGeoLocation;
-(void)StopGpsGeoLocation;
-(double)GetLatitude;
-(double)GetLongitude;
@end
//Implementation
#import "GeoLocation.h"
@implementation GeoLocation
+ (id)sharedGeoLocationManager {
static SGGI_GeoLocation *sharedGeoLocationManager = nil;
@synchronized(self) {//To safeguard threading issues
if (sharedGeoLocationManager == nil)
sharedGeoLocationManager = [[self alloc] init];
}
return sharedGeoLocationManager;
}
- (id)init {
if (self = [super init]) {
// someProperty = [[NSString alloc] initWithString:@"Default Property Value"];
}
return self;
}
-(double)GetLatitude
{
return latitude;
}
-(double)GetLongitude
{
return longitude;
}
-(void)StartGpsGeoLocation
{
locationManager=[[CLLocationManager alloc]init];
[locationManager setDelegate:self];
[locationManager setActivityType:CLActivityTypeFitness];//Pedestrian activity
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager startUpdatingLocation];
}
-(void)StopGpsGeoLocation
{
[locationManager stopUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
latitude=newLocation.coordinate.latitude;
longitude=newLocation.coordinate.longitude;
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
[manager stopUpdatingLocation];
NSLog(@"error%@",error);
switch([error code])
{
case kCLErrorNetwork: // general, network-related error
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Information" message:@"Please check your network connectivity or that you are not in airplane mode" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
}
break;
case kCLErrorDenied:{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Information" message:@"Please enable the localization services to further continue to work on this app." delegate:self cancelButtonTitle:@"Quit app" otherButtonTitles:nil, nil];
[alert show];
}
break;
default:
{
NSLog(@"GPS ErrorCode:%d",[error code]);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"GPS" message:@"Unknown network error." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
alert.tag=100;
// [alert show];
}
break;
}
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if([[alertView buttonTitleAtIndex:buttonIndex] isEqualToString:@"Quit app"])
{
exit(0);
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment