1
我有一個應用程序顯示了某些標籤上的當前位置。在viewDidLoad
我打電話[gps startUpdatingLocation];
(的CLLocationManager
實例),所以gps
調用相關方法:獲取地址時CLLocationManager stopUpdatingLocation,但地址標籤仍然爲空
- (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
標籤仍然是空的!爲什麼?
PS:sanitizedDescription
是返回一個簡單的方法「......」如果placemark.value
是nil
:
- (id)sanitizedDescription:(NSString *)obj
{
if (obj == nil)
{
obj = @"...";
return obj;
}
return obj;
}