【发布时间】: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
NSLog 在viewDidLoad 方法中的输出是0.0000,0.0000
locationManager:didUpdateToLocation:fromLocation: 委托方法没有被调用的原因可能是什么?
【问题讨论】:
-
你添加了 CLLocationManagerDelegate 协议了吗?
-
请检查我更新的 .h 文件代码
标签: ios objective-c cllocationmanager