2014-04-23 3 views
0

멀티 태스킹을 수행 한 후 초기로드 또는 다시 시작시 현재 위치를 찾을 수 없습니다.코어 위치가 현재 위치를 찾지 못함

ViewController.h

@interface ViewController : UIViewController <CLLocationManagerDelegate, ADBannerViewDelegate, BEMSimpleLineGraphDelegate> 

ViewController.m 또한 아래

@interface ViewController(){ 
CLLocationManager *locationManager; 
CLLocation *location; 
CLLocationCoordinate2D coordinate; 
int timeofday; 
NSString *cityName; 
NSMutableArray *ArrayOfValues; 
} 
@end 

:

다음
- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateLabels) name:UIApplicationWillEnterForegroundNotification object:nil]; 

// set up coordinates for current location 
locationManager = [[CLLocationManager alloc] init]; 
locationManager.delegate = (id)self; 
locationManager.distanceFilter = kCLDistanceFilterNone; 
locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
[locationManager startUpdatingLocation]; 

location = [locationManager location]; 

} 

내 업데이트 라벨 방법 (다른 코드가이 내가 가진 무엇 아래에서 내보기가 업데이트 됨) :

여기
- (void)updateLabels 
{ 

CLLocationCoordinate2D currentLocation = [location coordinate]; 

if (currentLocation.latitude) { 
    CLGeocoder *geocoder = [[CLGeocoder alloc] init] ; 
    [geocoder reverseGeocodeLocation:location 
        completionHandler:^(NSArray *placemarks, NSError *error) { 
         if (error){ 
          NSLog(@"Geocode failed with error: %@", error); 
          return; 
         } 
         CLPlacemark *placemark = [placemarks objectAtIndex:0]; 
         cityName = [NSString stringWithFormat:@"%@ is currently",placemark.locality]; 
        }]; 
    [self updateLabels]; 
} else { 
    NSLog(@"Could not find the location."); 
} 


} 

및은 cclocationmanager위한 2 위임 방법이다 : updateLabels에

#pragma mark - CLLocationManagerDelegate 

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{ 
NSLog(@"didFailWithError: %@", error); 
UIAlertView *errorAlert = [[UIAlertView alloc] 
          initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
[errorAlert show]; 
} 



- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
//NSLog(@"didUpdateToLocation: %@", newLocation); 
CLLocation *currentLocation = newLocation; 





     [self updateLabels]; 


    [locationManager stopUpdatingLocation]; 
    CLGeocoder *geocoder = [[CLGeocoder alloc] init] ; 
    [geocoder reverseGeocodeLocation:currentLocation 
        completionHandler:^(NSArray *placemarks, NSError *error) { 
         if (error){ 
          NSLog(@"Geocode failed with error: %@", error); 
          return; 
         } 
         // CLPlacemark *placemark = [placemarks objectAtIndex:0]; 

        }]; 




} 
+0

'[locationManager startUpdatingLocation];을'viewDidLoad'에서'viewWillAppear' 또는'viewDidAppear'로 옮겨 보았습니까? – Stonz2

+0

앱이 내 위치를 사용하도록 침투합니까? 설정 앱에서 위치 개인 정보 보호 설정을 확인합니다. –

+0

예. 테스트 장치와 시뮬레이터에서 실행됩니다 (열기 전에 선택한 사용자 지정 위치가 있고 위치를 계속 선택하면 앱을 실행할 때 확인 됨). – TerryG

답변

0

내 호가 부재 가변 위치에 설정된 값을 사용하고있다. 그러나 didUpdateToLocation 메소드가이 값을 설정하지 않습니다.

코드에 몇 가지 중단 점을 넣고 트리거 위치를 확인하십시오.

날씨 애플리케이션의 경우 더 거친 설정보다 시간이 오래 걸리는 kCLLocationAccuracyBest는 필요하지 않습니다. 당신은 당신의 대답을 훨씬 빨리 얻을 수 있습니다.

+0

내 라벨을 채우기 위해 하나의 위치 만 가져야합니까? 날씨 API에 API 요청을하기 위해 위치를 사용 중입니다. 계속 업데이트 할 필요가 없습니다. 나는 이것이 배터리 성능을 위해 더 낫다고 생각했다. – TerryG

+0

귀하의 앱이 한 번 작동해야한다고 말하고 싶습니다. 앱이 포 그라운드로 들어갔을 때 다시 시작하는 것에 대해 질문합니다. 한 번 작동합니까? –

+0

한 번 작동하지 않습니다. – TerryG

0

stopUpdatingLocation에 대한 호출을 제거해보십시오. 또한 didUpdateToLocation의 log 문을 주석 해제합니다.

locationManager:didUpdateToLocation:fromLocation: 메서드는 iOS 6에서 사용되지 않습니다. iOS 7에서는 호출되지 않을 수도 있습니다. 대신 'locationManager : didUpdateLocations :'를 사용해야합니다.

+0

stopUpdatingLocation에 대한 호출을 제거했지만 이제 '위치를 찾을 수 없습니다.'라는 반복 로그가 나타납니다. – TerryG

+0

업데이트에 위치를 설정하지 않았기 때문입니다. –

0

location (ViewDidLoad에있는 클래스 레벨 iVar이라고 가정합니다).

location = [locationManager location]; 

그 시점의 위치 관리자에는 위치가 없을 것으로 예상됩니다.

그런 다음 대리자 호출에서, 당신은 방법 레벨 바르를 업데이트 :

CLLocation *currentLocation = newLocation; 

이 변수는 내가 볼 수있는, 사용되지 않습니다. updateLabels 번은 다시 업데이트 된 적이없는 location을 다시 한번 호출합니다.

CLLocationCoordinate2D currentLocation = [location coordinate]; 

변경 대리자 :

location = newLocation; 

@Duncan C가 언급 한 바와 같이, 사용하는 위임 방법은, 당신이 정말로 어떤 경우에는 당신이 사용하는 것이 locationManager:didUpdateLocations:를 사용해야되지 않습니다 :

location = [locations lastObject];