我有一个应用程序,显示某些标签上的当前位置。在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.value
为nil
,则返回“...”:
- (id)sanitizedDescription:(NSString *)obj
{
if (obj == nil)
{
obj = @"...";
return obj;
}
return obj;
}
答案 0 :(得分:2)
问题已解决,address
标签太小,所以我只是在[address sizeToFit]
周期中再次调用if
:
if (address.text != NULL)
{
[address sizeToFit];
[gps stopUpdatingLocation];
}