CLLocationManager 委托方法 didUpdateToLocation 没有被调用

Posted

技术标签:

【中文标题】CLLocationManager 委托方法 didUpdateToLocation 没有被调用【英文标题】:CLLocationManager delegate method didUpdateToLocation not getting called 【发布时间】:2014-04-09 09:54:59 【问题描述】:

我有以下代码来获取我的纬度、经度、水平精度和高度。但是这个方法没有被调用,

-(void)locationManager:(CLLocationManager *)manage 
             didUpdateToLocation:(CLLocation *)newLocation 
             fromLocation:(CLLocation *)oldLocation;

我的代码,

//
//  CDViewController.m
//  Compass
//
//  Created by Naveen Kumar on 09/04/14.
//  Copyright (c) 2014 CamDon. All rights reserved.
//

#import "CDViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface CDViewController ()

@end

@implementation CDViewController

- (void)viewDidLoad

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.locationManager = [[CLLocationManager alloc] init];
    self.currentHeading = [[CLHeading alloc] init];
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    self.locationManager.headingFilter = 1;
    self.locationManager.delegate = self;
    [self.locationManager startUpdatingHeading];
    NSLog(@"%f",self.locationManager.location.coordinate.latitude);
    NSLog(@"%f",self.locationManager.location.coordinate.longitude);
    _startLocation = nil;


- (void)didReceiveMemoryWarning

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading

    self.currentHeading = newHeading;


    self.headingLabel.text = [NSString stringWithFormat:@"%d°", (int)newHeading.magneticHeading];


- (BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manager

    if(self.currentHeading == nil)
    
        return YES;
    
    else
    
        return NO;
    

#pragma mark -
#pragma mark CLLocationManagerDelegate

-(void)locationManager:(CLLocationManager *)manage didUpdateToLocation:(CLLocation *)newLocation
          fromLocation:(CLLocation *)oldLocation

    NSString *currentLatitude = [[NSString alloc]
                                 initWithFormat:@"%+.6f",
                                 newLocation.coordinate.latitude];
    _latitude.text = currentLatitude;

    NSString *currentLongitude = [[NSString alloc]
                                  initWithFormat:@"%+.6f",
                                  newLocation.coordinate.longitude];
    _longitude.text = currentLongitude;

    NSString *currentHorizontalAccuracy =
    [[NSString alloc]
     initWithFormat:@"%+.6f",
     newLocation.horizontalAccuracy];
    _horizontalAccuracy.text = currentHorizontalAccuracy;

    NSString *currentAltitude = [[NSString alloc]
                                 initWithFormat:@"%+.6f",
                                 newLocation.altitude];
    _altitude.text = currentAltitude;

    NSString *currentVerticalAccuracy =
    [[NSString alloc]
     initWithFormat:@"%+.6f",
     newLocation.verticalAccuracy];
    _verticalAccuracy.text = currentVerticalAccuracy;

    if (_startLocation == nil)
        _startLocation = newLocation;

    CLLocationDistance distanceBetween = [newLocation
                                          distanceFromLocation:_startLocation];

    NSString *tripString = [[NSString alloc]
                            initWithFormat:@"%f",
                            distanceBetween];
    _distance.text = tripString;

-(void)locationManager:(CLLocationManager *)manager
      didFailWithError:(NSError *)error


@end

我的 .h 文件

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface CDViewController : UIViewController<UIApplicationDelegate,CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *headingLabel;
@property (nonatomic,retain) CLLocationManager *locationManager;
@property (nonatomic, retain) CLHeading * currentHeading;
@property (strong, nonatomic) IBOutlet UILabel *latitude;
@property (strong, nonatomic) IBOutlet UILabel *longitude;
@property (strong, nonatomic) IBOutlet UILabel *horizontalAccuracy;
@property (strong, nonatomic) IBOutlet UILabel *altitude;
@property (strong, nonatomic) IBOutlet UILabel *verticalAccuracy;
@property (strong, nonatomic) IBOutlet UILabel *distance;
@property (strong, nonatomic) CLLocation *startLocation;
@end

NSLogviewDidLoad 方法中的输出是0.0000,0.0000

locationManager:didUpdateToLocation:fromLocation: 委托方法没有被调用的原因可能是什么?

【问题讨论】:

你添加了 CLLocationManagerDelegate 协议了吗? 请检查我更新的 .h 文件代码 【参考方案1】:

你只是调用[self.locationManager startUpdatingHeading];,所以你会得到locationManager:didUpdateHeading:的回调。

如果你想回调到locationManager:didUpdateLocations:,你应该在位置管理器上调用startUpdatingLocation

【讨论】:

我应该在哪里称呼它?怎么称呼?你能解释一下吗? 非常感谢,这个答案很好【参考方案2】:

实现CLLocationManager的以下委托方法。另外请注意,用户位置坐标将仅在CLLocationManager的委托中获取,而不是在viewDidLoad中获取

 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 
    CLLocation *newLocation = [locations lastObject];
   NSLog(@"userLat: %f ,userLon:%f",newLocation.coordinate.latitude,newLocation.coordinate.longitude);

   NSLog(@" reading horizontal accuracy :%f", newLocation.horizontalAccuracy);
    

【讨论】:

我可以从这里得到海拔高度吗?水平精度呢? 你应该从这个代表那里得到水平精度。请检查我更新的答案。 @AppleDelegate 非常感谢。你节省了我很多时间。【参考方案3】:

再添加一行 -

[self.locationManager startUpdatingLocation];

【讨论】:

我应该在哪里添加?在 viewDidLoad 中? 感谢@pankaj,快速回答

以上是关于CLLocationManager 委托方法 didUpdateToLocation 没有被调用的主要内容,如果未能解决你的问题,请参考以下文章

CLLocationManager 如何根据 iOS 版本调用正确的委托方法

XCTest CLLocationManager 的委托方法不会被调用

CLLocationManager委托方法未被调用

是否有任何方法可以在我的应用程序中不使用 CLLocationManager 委托来获取当前位置

故意调用 CLLocationManager 委托的 didFailWithError:

CLLocationManager 的委托函数“didRangeBeacon”没有找到任何信标?