2014-04-23 74 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委託方法:

#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

您對updateLabels呼叫是使用在成員變量的位置設置的值。但是,您的didUpdateToLocation方法不設置此值。

在代碼中放入一些斷點並查看它觸發的位置。

請注意,對於天氣應用,您不需要kCLLocationAccuracyBest,它比粗糙設置花費的時間要多。你會更快地得到你的答案。

+0

不應該只需要獲取一個位置以填充我的標籤?我正在使用該位置向天氣API發出API請求。我不需要不斷更新。我認爲這對電池性能更好。 – TerryG

+0

我會說你的應用程序應該工作一次。你的問題談到恢復,當應用程序進入前臺。它甚至可以工作一次嗎? –

+0

它甚至不能工作一次。 – TerryG

0

嘗試刪除對stopUpdatingLocation的調用。還取消註釋didUpdateToLocation中的日誌語句。

請注意,在iOS 6中不推薦使用方法locationManager:didUpdateToLocation:fromLocation:。它甚至可能不會在iOS 7中調用。您應該使用`locationManager:didUpdateLocations:'代替。

+0

我刪除了對stopUpdatingLocation的調用,現在我只是得到'找不到位置'的重複日誌。 – TerryG

+0

這是因爲您未在更新中設置位置 –

0

您正在設置location,我認爲這是一個級別的iVar,在ViewDidLoad

location = [locationManager location]; 

我期望那個位置的經理沒有位置。

然後,在委託調用,您正在更新的方法級別伊娃:

CLLocation *currentLocation = newLocation; 

這個變量沒用過,我可以看到。您再次致電updateLabels是指從未更新過的location

CLLocationCoordinate2D currentLocation = [location coordinate]; 

更改委託:

location = newLocation; 

由於@Duncan C中規定,您所使用的委託方法已過時,你應該使用locationManager:didUpdateLocations:在這種情況下,你可以使用:

location = [locations lastObject];