iOS 5,等待位置

时间:2012-06-13 08:30:01

标签: ios cllocationmanager

我有这种方法来定位用户,然后根据他所在的国家/地区更改一些数字:

- (void) localizing {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startUpdatingLocation];
locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
DbOperations *dbOp = [[DbOperations alloc] init];
geoCoder = [[CLGeocoder alloc] init];
[self.geoCoder reverseGeocodeLocation: locationManager.location completionHandler: 
 ^(NSArray *placemarks, NSError *error) {

     //Get nearby address
     CLPlacemark *placemark = [placemarks objectAtIndex:0];

     //String to hold address
     countryCode = [placemark.addressDictionary valueForKey:@"CountryCode"];
     country = [placemark.addressDictionary valueForKey:@"Country"];
     NSString *s2 = NSLocalizedString(@"You're currently in %@", nil);
     lblLocation.text = [NSString stringWithFormat:s2, self.country];
     [dbOp UsefulNumbers:self.countryCode];
     NSUserDefaults *loc = [NSUserDefaults standardUserDefaults];
     [loc setObject:country forKey:@"Country"];
     [loc setObject:countryCode forKey:@"CountryCode"];
     [loc synchronize];

 }];
[locationManager stopUpdatingLocation];
NSUserDefaults *loc = [NSUserDefaults standardUserDefaults];
if ([loc objectForKey:@"Country"] == NULL) {
    btnUno.enabled = NO;
    btnDue.enabled = NO;
    btnTre.enabled = NO;
    lblLocation.text = NSLocalizedString(@"Unable to locate you", nil);
} 


}

该方法由'viewDidLoad'方法调用,但是我第一次运行应用程序时无法找到它。 如果我打开地图,让它得到修复,然后打开我的应用程序,一切都很顺利。 我想也许我没有给应用程序足够的时间来获得GPS修复,或者我只是做错了。

有什么建议吗?

编辑:修改了代码,但还没有工作。

- (id) init {
self = [super init];
if (self != nil) {
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
}
return self;
}

- (void) viewDidLoad {
locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
[locationManager startUpdatingLocation];
[super viewDidLoad];
}


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

DbOperations *dbOp = [[DbOperations alloc] init];
geoCoder = [[CLGeocoder alloc] init];
[self.geoCoder reverseGeocodeLocation: locationManager.location completionHandler: 
 ^(NSArray *placemarks, NSError *error) {

     //Get nearby address
     CLPlacemark *placemark = [placemarks objectAtIndex:0];

     //String to hold address
     countryCode = [placemark.addressDictionary valueForKey:@"CountryCode"];
     country = [placemark.addressDictionary valueForKey:@"Country"];
     NSString *s2 = NSLocalizedString(@"You're currently in %@", nil);
     lblLocation.text = [NSString stringWithFormat:s2, self.country];
     [dbOp UsefulNumbers:self.countryCode];
     NSUserDefaults *loc = [NSUserDefaults standardUserDefaults];
     [loc setObject:country forKey:@"Country"];
     [loc setObject:countryCode forKey:@"CountryCode"];
     [loc synchronize];
     NSLog(@"Country: %@", country);

 }];
NSUserDefaults *loc = [NSUserDefaults standardUserDefaults];
if ([loc objectForKey:@"Country"] == NULL) {
    btnUno.enabled = NO;
    btnDue.enabled = NO;
    btnTre.enabled = NO;
    lblLocation.text = NSLocalizedString(@"unable to locate", nil);
} 

}

方法locationManager:didUpdateToLocation:fromLocation不会被调用。

1 个答案:

答案 0 :(得分:2)

您应该将反向地理编码的代码放在CLLocationManager的委托方法中:

– locationManager:didUpdateToLocation:fromLocation:

您的当前设置正尝试在从位置管理器调度更新之前对地理位置进行反向地理编码。

考虑在init方法中设置CLLocationManager,然后在尝试反向地理编码任何内容之前等待代理被调用。