1

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

- (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 不会被调用。

4

1 回答 1

2

You should put your code for reverse geocoding in the delegate method for CLLocationManager:

– locationManager:didUpdateToLocation:fromLocation:

Your current setup is trying to reverse geocode a location before an update has been dispatched from the location manager.

Consider setting up your CLLocationManager in your init method and then waiting for the delegate to be called before attempting to reverse geocode anything.

于 2012-06-13T08:51:34.723 回答