2014-04-23 67 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];