【问题标题】:CLLocationManager stopUpdatingLocation when address is obtained, but address label is still empty获取地址时CLLocationManager stopUpdatingLocation,但地址标签仍为空
【发布时间】:2013-11-20 17:29:15
【问题描述】:

我有一个在某些标签上显示当前位置的应用。在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;
}

【问题讨论】:

    标签: iphone objective-c ios7 cllocationmanager


    【解决方案1】:

    问题解决了,address标签太小,所以我在if循环中再次调用[address sizeToFit]

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-21
      • 1970-01-01
      • 2011-09-19
      • 2017-12-22
      • 1970-01-01
      相关资源
      最近更新 更多