Skip to content

Instantly share code, notes, and snippets.

@eiffelqiu
Created May 25, 2011 00:38
Show Gist options
  • Save eiffelqiu/990073 to your computer and use it in GitHub Desktop.
Save eiffelqiu/990073 to your computer and use it in GitHub Desktop.
Get GPS From using CLLocationManager
// view GPSViewController.h file
// Step 1: add CoreLocation framework in Project
// Step 2: include the CoreLocation.h file
// implement the following codes
#import <CoreLocation/CoreLocation.h>
@interface GPSViewController : UIViewController <CLLocationManagerDelegate> {
CLLocationManager *locationManager;
}
@end
// view controller .m file
#import "GPSViewController.h"
@implementation RRScrollView
// 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];
}
- (void)dealloc {
[super dealloc];
}
@end
@MacKaSL
Copy link

MacKaSL commented Jun 27, 2017

what does this do?
double decimal = fabs(newLocation.coordinate.latitude - degrees);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment