2012-04-25 68 views
4

我正在使用核心位置框架來定位設備,並且一旦locationManager:didUpdateToLocation:fromLocation:方法與位置一起被調用,我將停止跟蹤用戶。核心位置接受提示後不更新

但是,我第一次啓動應用程序(從全新安裝)。當我向我的位置管理器發送startUpdatingLocation消息時,用戶會收到警報以接受或拒絕位置服務。

當我接受跟蹤沒有開始,只有當我走開,回來這個視圖控制器時startUpdatingLocation被再次調用該通知,開始在未來

我採取的LocationManager:didChangeAuthorizationStatus:思考當用戶接受(或拒絕)位置服務時,這會得到消息,但事實並非如此。

只要位置服務消息被解除,任何人都可以指示我正確更新位置的方向嗎?

謝謝。

的代碼示例更新

我有一個單例類,它封裝了我的邏輯,當請求用戶位置,在對第CLLocation的時間戳檢查的想法是,如果它太舊了,開始跟蹤傳遞消息是,這延遲加載我CLLocationManager伊娃,

-(void)startTracking{ 

    if(!self.locationManager) 
     _locationManager = [[CLLocationManager alloc] init]; 


    if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied){ 
     [self invalidateUserLocation]; 
    }else{ 
     self.locationManager.delegate = self; 
     [self.locationManager startUpdatingLocation]; 
    } 

} 

新位置接收:

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation 
{ 
    _userLocation = [newLocation retain]; 

    NSDictionary * info = [NSDictionary dictionaryWithObject:self.userLocation 
                 forKey:@"userLocation"]; 

    [[NSNotificationCenter defaultCenter] postNotificationName:kUserLocationFound 
                 object:self 
                 userInfo:info]; 
    [self stopTracking]; 

} 

停止跟蹤:

-(void)stopTracking{ 
    if(!self.locationManager) 
     return; 

    [self.locationManager stopUpdatingLocation]; 
    self.locationManager.delegate = nil; 
} 

當我有一個需要用戶位置的視圖控制器時,我像我這樣在我的單例對象上調用userLocation。如果是最近的,我返回CLLocation,否則返回零並重新開始。注意我收到第一次更新時停止跟蹤。但是,第一次運行,我得到了警報視圖,根本沒有任何跟蹤。

- (CLLocation*)userLocation 
{ 
    if(_userLocation.coordinate.latitude == 0 && _userLocation.coordinate.longitude == 0){ 
     [self startTracking]; 
     return nil; 
    }else{ 
     NSDate* timeNow = [NSDate date]; 
     NSTimeInterval interval = [timeNow timeIntervalSinceDate:_userLocation.timestamp]; 
     if(interval >10) 
      [self startTracking]; 

     return _userLocation; 
    } 
} 
+1

你在你的CLLocationManager實例上調用startUpdatingLocation嗎? – jmstone617 2012-04-25 16:28:19

+0

沒錯,我在CLLocationManager實例上調用它。 – Daniel 2012-04-25 16:34:31

+0

你能給我看一些代碼嗎? – jmstone617 2012-04-25 16:38:45

回答

16

你嘗試調用從CLLocationManagerDelegate– locationManager:didChangeAuthorizationStatus:

我猜你會在視圖控制器加載時調用startTracking。這是通過詢問是否可以的警報來規避的。此時,起始定位消息將不會再次被調用,因此致電didChangeAuthorizationStatus,您可以撥打您的startTracking方法。

喜歡的東西:

-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status { 

    if (status == kCLAuthorizationStatusDenied) { 
     //location denied, handle accordingly 
    } 
    else if (status == kCLAuthorizationStatusAuthorized) { 
     //hooray! begin startTracking 
    } 

} 

如果不是的話,讓我知道。

+0

我剛剛獲得(CLAuthorizationStatus)狀態爲0,沒有跟蹤位置發生異常 – 2015-06-24 07:25:07