CLLocationManager委托方法didUpdateToLocation未被调用

时间:2014-04-09 09:54:59

标签: ios objective-c cllocationmanager

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

-(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

NSLog方法中viewDidLoad的输出为0.0000,0.0000

locationManager:didUpdateToLocation:fromLocation:委托方法未被调用的原因是什么?

3 个答案:

答案 0 :(得分:3)

您只是致电[self.locationManager startUpdatingHeading];,因此您会收到locationManager:didUpdateHeading:的回调。

如果您想要回复locationManager:didUpdateLocations:,您应该在位置管理员处拨打startUpdatingLocation

答案 1 :(得分: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);
    }

答案 2 :(得分:1)

再添加一行 -

[self.locationManager startUpdatingLocation];