获取地址时CLLocationManager stopUpdatingLocation,但地址标签仍为空

时间:2013-11-20 17:29:15

标签: iphone objective-c ios7 cllocationmanager

我有一个应用程序,显示某些标签上的当前位置。在viewDidLoad我致电[gps startUpdatingLocation];CLLocationManager的实例),因此gps调用相对方法:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    //NSLog(@"didUpdateToLocation: %@", newLocation);
    currentLocation = newLocation;

    if (currentLocation != nil) {
        longitude.text = [NSString stringWithFormat:@"%.3f", currentLocation.coordinate.longitude];
        latitude.text = [NSString stringWithFormat:@"%.3f", currentLocation.coordinate.latitude];
    }

    NSLog(@"Resolving the Address");
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
        if (error == nil && [placemarks count] > 0) {
            placemark = [placemarks lastObject];
            [address sizeToFit];
            address.text = [NSString stringWithFormat:@"%@, %@\n%@ %@\n%@",
                            [self sanitizedDescription:placemark.thoroughfare],
                            [self sanitizedDescription:placemark.subThoroughfare],
                            [self sanitizedDescription:placemark.postalCode],
                            [self sanitizedDescription:placemark.locality],
                            [self sanitizedDescription:placemark.country]];

            if (address.text != NULL)
            {
                [gps stopUpdatingLocation];
            }
        } else {
            NSLog(@"%@", error.debugDescription);
        }
    } ];

}

因此,当获得地址时,gps必须stopUpdatingLocation。一切正常,但应用内address标签仍为空!为什么呢?

P.S。:sanitizedDescription是一个简单的方法,如果placemark.valuenil,则返回“...”:

- (id)sanitizedDescription:(NSString *)obj
{
    if (obj == nil)
    {
        obj = @"...";
        return obj;
    }

    return obj;
}

1 个答案:

答案 0 :(得分:2)

问题已解决,address标签太小,所以我只是在[address sizeToFit]周期中再次调用if

        if (address.text != NULL)
        {
            [address sizeToFit];
            [gps stopUpdatingLocation];
        }