【发布时间】: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.value 是 nil,则返回“...”:
- (id)sanitizedDescription:(NSString *)obj
{
if (obj == nil)
{
obj = @"...";
return obj;
}
return obj;
}
【问题讨论】:
标签: iphone objective-c ios7 cllocationmanager