未调用iOS CLLocationManagerDelegate didUpdateToLocation

时间:2014-07-25 17:53:45

标签: ios location core-location cllocationmanager

我正在创建一个使用核心位置的应用,这是我的代码:

.h:

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>    
@interface ViewController : UIViewController
<CLLocationManagerDelegate>    
@property(strong,nonatomic) CLLocationManager *manager;    
@end

.m:

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()
@end
@implementation ViewController
@synthesize manager;
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    if (!self.manager)
    {
        self.manager=[CLLocationManager new];
    }
    self.manager.delegate = self;
    self.manager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
    [self.manager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation * currentLocation = (CLLocation *)[locations lastObject];
    NSLog(@"Location: %@", currentLocation);
    if (currentLocation != nil)
    {
        NSLog([NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]);
        NSLog([NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]);
    }
}
@end

问题是didUpdateToLocation没有被调用。 我尝试在其中放置一个断点,没有任何反应。

2 个答案:

答案 0 :(得分:1)

要使iOS位置跟踪正常工作,以下是先决条件,顺序也很重要:

  1. 将您的班级定义为CLLocationManagerDelegate
  2. 实例化CLLocationManager个实例。
  3. 将其delegate设置为self
  4. 设置其各种属性(准确度等)
  5. 打算致电[CLLocationManagerDelegate startUpdatingLocation]
  6. 在您的委托方法didUpdateLocations中收听位置更新,在指定为CLLocationManagerDelegate的类中实施。
  7. 使用您的代码,以下是您需要纠正的内容:

    -(IBAction)submit
    {   
        [self.manager startUpdatingLocation];  
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        if (!self.manager)
        {
            self.manager=[CLLocationManager new];
        }
        self.manager.delegate = self;
        self.manager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;     
    }
    

    简而言之,在告诉它开始更新位置之前,您需要实例化它并正确设置其属性。目前你正在以相反的方式做到这一点。

    <强>更新

    此外,自iOS 6起,您的委托方法didUpdateToLocation已弃用。您必须将其替换为newer method,如下所示:

    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    {    
        CLLocation * currentLocation = (CLLocation *)[locations lastObject];
        NSLog(@"Location: %@", currentLocation);
        if (currentLocation != nil) 
        {
            self.latitude.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
            self.longitude.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
        }
    }
    

答案 1 :(得分:0)

您需要将这些密钥添加到Info.plist中:

NSLocationWhenInUseUsageDescription

NSLocationAlwaysUsageDescription

如果iOS8:

还请求您要授权的类型
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
        {
            [self.locationManager requestWhenInUseAuthorization];
        }

        [self.locationManager startUpdatingLocation];

因此,系统将显示警告&#34;允许&#34; app&#34;访问你的位置......?允许将启动您的位置管理员并将调用didUpdateLocations