【问题标题】:iOS CLLocationManagerDelegate didUpdateToLocation is not being callediOS CLLocationManagerDelegate didUpdateToLocation 未被调用
【发布时间】:2014-07-25 17:53:45
【问题描述】:

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

.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 没有被调用。 我尝试在其中放置一个断点,但什么也没发生。

【问题讨论】:

    标签: ios location core-location cllocationmanager


    【解决方案1】:

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

    1. 将您的类定义为CLLocationManagerDelegate
    2. 实例化CLLocationManager 实例。
    3. 将其delegate 设置为self
    4. 设置其各种属性(精度等)
    5. 如需,请致电[CLLocationManagerDelegate startUpdatingLocation]
    6. 在您的委托方法 didUpdateLocations 中收听位置更新,在指定为 CLLocationManagerDelegate 的类中实现。

    使用您的代码,以下是您需要更正的地方:

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

    简而言之,在告诉它开始更新位置之前,您需要实例化它并正确设置其属性。目前您正在以相反的方式进行操作。

    更新

    此外,您的委托方法 didUpdateToLocation 自 iOS 6 起已弃用。您必须将其替换为 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];
        }
    }
    

    【讨论】:

    • 谢谢 Nirav 试过它没有用对不起我只是第一次使用它的核心位置所以也许我犯了一个非常愚蠢的错误我会更新我的代码如果你会很高兴再次检查它
    【解决方案2】:

    您需要将这些键添加到您的 Info.plist:

    NSLocationWhenInUseUsageDescription
    
    NSLocationAlwaysUsageDescription
    

    如果是iOS8,还要请求你要授权的类型:

    if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
            {
                [self.locationManager requestWhenInUseAuthorization];
            }
    
            [self.locationManager startUpdatingLocation];
    

    所以系统会显示警报“允许“应用”访问您的位置...?允许将启动您的位置管理器并调用didUpdateLocations

    【讨论】:

      猜你喜欢
      • 2015-03-27
      • 2014-11-30
      • 1970-01-01
      • 2011-11-01
      • 2012-12-03
      • 1970-01-01
      • 2014-04-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多