iOS 5,等待定位

标签 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”方法调用,但我第一次运行该应用程序时无法定位。 如果我打开 map ,让它得到修复,然后打开我的应用程序,一切正常。 我想也许我没有给该应用程序足够的时间来获取 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 没有被调用。

最佳答案

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

– locationManager:didUpdateToLocation:fromLocation:

您当前的设置试图在位置管理器发送更新之前对位置进行反向地理编码。

考虑在您的 init 方法中设置您的 CLLocationManager,然后在尝试反转任何地理编码之前等待委托(delegate)被调用。

关于iOS 5,等待定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11010964/

相关文章:

ios - 使用 swift 4 进行 JSON 编码/解码

ios - 如何快速地为具有内部类型的协议(protocol)定义类型

ios - 无法将自定义颜色设置为导航栏项目 iOS Swift

iphone - 如何关闭定位服务?

ios - 如何从 iOS 中的默认注释 Mapkit 获取位置名称

ios - 如何从 FQL 查询访问信息

ios - 按需重新加载 UITabBarController

cllocationmanager - 在 Swift 中实现 CLLocationManagerDelegate 方法

ios - CLLocationManager didEnterRegion、didExitRegion 和 didDetermineState 的委托(delegate)方法未在 iOS 8.4(设备)上调用

ios - 跟踪用户位置与其他坐标之间的距离