How to get the current location or GPS coordinates with Core Location?
+2
A:
Hi,
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
anka
2010-06-24 18:43:32