dontWatchMyProfile
dontWatchMyProfile

Reputation: 46370

How to get the current location or GPS coordinates with Core Location?

How do I get the current location or GPS coordinates with the Core Location framework?

Upvotes: 1

Views: 2342

Answers (1)

anka
anka

Reputation: 3857

first of all you need a class implementing the protocol CLLocationManagerDelegate. So at least you need to implement the method:

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
 ...
}

After that create an instance of CLLocationManager, set the delegate and start updating the location:

CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate        = self;  //SET YOUR DELEGATE HERE
locationManager.desiredAccuracy = kCLLocationAccuracyBest; //SET THIS TO SPECIFY THE ACCURACY
[locationManager startUpdatingLocation];

After calling startUpdatingLocation, your implementation of locationManager: didUpdateToLocation: fromLocation gets called as soon as a location fix occurs. The parameter newLocation contains your actual location. BUT the location manager will fix a location as soon as possible, even if your specifed accuracy is not given. For this case you have to check the accuracy of the new location by your own.

cheers, anka

Upvotes: 6

Related Questions