2011-08-30 31 views
1

我使用一個iPhone 4運行的代碼初始用戶位置:iPhone - 與CCLocationManager

- (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米跑步?

回答

2

您可以通過自己踢一下委託方法來「填充」它。

- (void) start 
{ 
    self.view.hidden = NO; 
    [self.locationMgr startUpdatingLocation]; 
    [self.locationMgr startUpdatingHeading]; 
    [self locationManager: self.locationMgr didUpdateToLocation: [self.locationMgr currentLocation] fromLocation: nil]; 
} 
0

你在哪裏打電話開始:?你應該首先修復而不移動。這是一個異步回調,因此可能需要時間。

理想情況下,你應該在init被調用startUpdateLocation/viewDidLoad中,然後讀取它在locationUpdate:

- (void)locationUpdate:(CLLocation *)location { 

// Read location 

}