我的代码如下:
#import "radSecondViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface radSecondViewController ()
@end
CLLocationManager *locationManager = nil;
@implementation radSecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *newLocation = [locations lastObject];
CLLocation *oldLocation;
if (locations.count > 1) {
oldLocation = [locations objectAtIndex:locations.count-2];
} else {
oldLocation = nil;
}
NSLog(@"didUpdateToLocation %@ from %@", newLocation, oldLocation);
MKCoordinateRegion userLocation = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 1500.0, 1500.0);
}
//snipped memory dump
@end
正如你所看到的,我试图让控制台在每次发生时都记录坐标的变化。
然而,当我构建应用程序时,没有任何内容输出到控制台。当我向locationManager:updateLocation...
行添加断点时,它们永远不会被击中。
我已将位置设置为模拟器中的多个位置(Apple,Freeway Drive等)。
有没有人遇到类似的问题并找到了修复方法?或者这是我正在犯的新手错误?我是Xcode和Objective C的新手。
感谢您的帮助!
答案 0 :(得分:1)
您尚未设置位置管理员的delegate
财产。
locationManager.delegate = self;
并将您的课程扩展名更新为:
@interface radSecondViewController () <CLLocationManagerDelegate>
@end
然后换行:
CLLocationManager *locationManager = nil;
位于@imlpementation
区块内:
@implementation radSecondViewController {
CLLocationManager *locationManager = nil;
}
正如您所知,它被声明为全局变量而不是实例变量。