Skip to content

Instantly share code, notes, and snippets.

@codeswimmer
Created January 2, 2013 19:53
Show Gist options
  • Select an option

  • Save codeswimmer/4437381 to your computer and use it in GitHub Desktop.

Select an option

Save codeswimmer/4437381 to your computer and use it in GitHub Desktop.
iOS: How to Use Core Location Services To Acquire A Current Location All The Time
If you need to acquire your current location all the time, you need to have a delegate method to receive this information and parse this location to acquire latitude and longitude value.
B sure to implement CLLocationManagerDelegate and create a property something like this in your .h file.
CLLocationManager *location_mgr;
And set properties.
@property (nonatomic, retain) CLLocationManager *location_mgr;
Instantiate you coreLocation manager and set delegate then have it start updating location.
self.location_mgr = [[[CLLocationManager alloc] init]autorelease];
[location_mgr setDelegate:self];
[location_mgr startUpdatingLocation];
Implement the Delegate methods to acquire updated location everytime or there is an error acquireing locations.
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
/* do something here */
}
and
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
/* do something here*/
}
Note that CCLocation has an object that gathers the latitude and longitude. So if you have location updates, you can get latitude and longitude with the code below inside your didUpdateToLocation: delegate method.
NSLog(@"Current Location: Latitude(%f), Longitude(%f)",newLocation.coordinate.latitude, newLocation.coordinate.longitude);
Be sure to release your CoreLocationManager so have this in your dealloc.
self.location_mgr = nil;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment