Home > CodeSnippets, iOS, Objective-C > Location via GPS with iOS

Location via GPS with iOS

First add the Core Location framework:

  • right click on the “Frameworks” group and go to Add > Existing Frameworks…
  • select the CoreLocation.framework

Then you can use the framework in your app:


#import <CoreLocation/CoreLocation.h>

@interface MyLocation : NSObject <CLLocationManagerDelegate> {
	CLLocationManager *locationManager;
}
@end

#import "MyLocation.h"

@implementation MyLocation

- (id) init {
   if ((self = [super init])) {
      // Custom initialization
      locationManager = [[CLLocationManager alloc] init];
      locationManager.delegate = self;
      locationManager.distanceFilter = kCLDistanceFilterNone;
      locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
     [locationManager startUpdatingLocation];
   }
   return self;
}

- (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 *lat = [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 *lon = [NSString stringWithFormat:@"%d° %d' %1.4f\"", 
                         degrees, minutes, seconds];
}
@end
Advertisements
Categories: CodeSnippets, iOS, Objective-C
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: