我正在使用iPhone 4上运行的代码:
- (id)init
{
self = [super initWithNibName:@"OfflineView" bundle:nil]; // ok, not perfect but for test, that works fine
if (self) {
self.locationMgr = [[CLLocationManager alloc] init];
self.locationMgr.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
self.locationMgr.distanceFilter = kCLDistanceFilterNone;
self.locationMgr.headingFilter = kCLHeadingFilterNone;
self.locationMgr.delegate = self;
}
return self;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
// do things
}
// triggered when showing the view, first call here after the init
- (void) start
{
self.view.hidden = NO;
[self.locationMgr startUpdatingLocation];
[self.locationMgr startUpdatingHeading];
}
但是委托方法没有被触发。
只有手机移动时才会触发。
当视图出现时,我如何用一个有效的用户位置初始化我的进程,而不要求我的用户在能够做某事之前摇晃它的手机并进行100米的运行?
您可以通过自己踢一次委托方法来"启动"它。
- (void) start
{
self.view.hidden = NO;
[self.locationMgr startUpdatingLocation];
[self.locationMgr startUpdatingHeading];
[self locationManager: self.locationMgr didUpdateToLocation: [self.locationMgr currentLocation] fromLocation: nil];
}
在哪里调用start:?你应该在不动的情况下接受第一次治疗。这是一个异步回调,因此可能需要一些时间。
理想情况下,你应该在init/viewDidLoad中调用startUpdateLocation,然后在locationUpdate:
中读取它- (void)locationUpdate:(CLLocation *)location {
// Read location
}