2011-05-27 26 views
0

一個更糟糕的一天上帝知道什麼,一旦我點擊刷新按鈕,這一點,但委託方法CLLocation Mangaer問題不叫,這個按鈕我的點擊問位置管理器更新位置信息iphone:CLLocation經理的委託方法不叫上一個按鈕,點擊

-(void) viewWillAppear:(BOOL)animated{ 


locationManager=[[CLLocationManager alloc] init]; 
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; 
locationManager.delegate = self; 
[locationManager startUpdatingLocation]; 

}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{ 

NSLog(@"failed"); 

}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{ 


NSLog(@"current lat= %f and long=%f ", newLocation.coordinate.latitude, newLocation.coordinate.longitude); 

NSDate *eventDate = newLocation.timestamp; 
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow]; 

if (abs(howRecent) < 10) { 

    [lat setText:[ NSString stringWithFormat:@"%f", newLocation.coordinate.latitude]]; 
    [lon setText:[ NSString stringWithFormat:@"%f", newLocation.coordinate.longitude]]; 

} 

else { 
    [lat setText:@"Not changed"]; 
    [lon setText:@"Not changed"]; 
} 

} 

    -(IBAction) refrechLoc{ 

NSLog(@"updating location"); 
[locationManager startUpdatingLocation]; 

} 請幫我解決這個問題。

在此先感謝

回答

1

它似乎開始更新viewWillAppear,所以它不會更新位置,直到它不被stopUpdatingLocation停止。如果你想重新開始更新你應該叫stopUpdatingLocationstartUpdatingLocation

-(IBAction) refrechLoc{ 
    NSLog(@"updating location"); 
    [locationManager stopUpdatingLocation]; 
    [locationManager startUpdatingLocation]; 
} 

恕我直言,存在位置更新重啓沒必要在一般

+0

是啊,這是相當該死的沒用。爲什麼建議它,如果沒有必要這樣做? – 2011-05-27 11:04:49

+0

我無法想象爲什麼應該重新開始更新,但也許有提供示例代碼背後的邏輯? :) – knuku 2011-05-27 11:35:21

0

你誤會了怎麼CLLocationManager作品。當CLLocationManager有一個新的位置時,代表被呼叫。 [locationManager startUpdatingLocation];只是告訴位置管理員開始更新位置,所以不需要再調用兩次。

編輯:爲了清楚起見:當位置被更新時,即設備移動時,代理被調用。