开发者

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

开发者 https://www.devze.com 2023-01-04 20:33 出处:网络
How do I get the current location or GPS coordinates with the Core Lo开发者_开发技巧cation framework?first of all you need a class implementing the protocol CLLocationManagerDelegate. So at least you

How do I get the current location or GPS coordinates with the Core Lo开发者_开发技巧cation framework?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消