核心位置找不到当前位置

时间:2014-04-23 13:25:47

标签: ios objective-c core-location cllocationmanager

在多任务处理时,初始加载或恢复时未找到当前位置。这就是我所拥有的:

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];

                   }];




}

3 个答案:

答案 0 :(得分:0)

您对updateLabels的调用是使用成员变量位置中设置的值。但是,您的didUpdateToLocation方法未设置此值。

在代码中加入一些断点并查看它的触发位置。

注意,对于天气应用程序,您不需要kCLLocationAccuracyBest,这比较粗略的设置需要更多时间。你会更快地得到答案。

答案 1 :(得分:0)

尝试删除对stopUpdatingLocation的调用。同时取消注释didUpdateToLocation中的日志语句。

请注意,iOS 6中不推荐使用方法locationManager:didUpdateToLocation:fromLocation:。它甚至可能在iOS 7中不被调用。您应该使用`locationManager:didUpdateLocations:'代替。

答案 2 :(得分:0)

您正在location设置ViewDidLoad,我认为这是一个类级iVar。

location = [locationManager location];

我希望那个位置的经理没有位置。

然后,在委托调用中,您正在更新方法级别iVar:

CLLocation *currentLocation = newLocation;

这个变量从未使用过,我可以看到。您的updateLabels电话再次引用从未更新的location

CLLocationCoordinate2D currentLocation = [location coordinate];

将委托更改为:

location = newLocation;

正如@Duncan C所述,您使用的委托方法已弃用,您应该使用locationManager:didUpdateLocations:,在这种情况下您将使用:

location = [locations lastObject];
相关问题