我正在使用反向地理编码,但我正在尝试分离属性。例如,我希望在其自己的文本字段中显示邮政编码。
这是我尝试的但我在if([placemarks count]> 0)
的断点- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
// Make sure this is a recent location event
CLLocation *newLocation = [locations lastObject];
NSTimeInterval eventInterval = [newLocation.timestamp timeIntervalSinceNow];
if(abs(eventInterval) < 30.0)
{
// Make sure the event is valid
if (newLocation.horizontalAccuracy < 0)
return;
// Instantiate _geoCoder if it has not been already
if (_geocoder == nil)
_geocoder = [[CLGeocoder alloc] init];
//Only one geocoding instance per action
//so stop any previous geocoding actions before starting this one
if([_geocoder isGeocoding])
[_geocoder cancelGeocode];
[_geocoder reverseGeocodeLocation: newLocation
completionHandler: ^(NSArray* placemarks, NSError* error)
{
if([placemarks count] > 0)
{
CLPlacemark *foundPlacemark = [placemarks objectAtIndex:0];
self.address.text =
[NSString stringWithFormat:@"%@",
foundPlacemark.description];
self.zip.text =
[NSString stringWithFormat:@"%@",
foundPlacemark.postalCode];
}
else if (error.code == kCLErrorGeocodeCanceled)
{
NSLog(@"Geocoding cancelled");
}
else if (error.code == kCLErrorGeocodeFoundNoResult)
{
self.address.text=@"No geocode result found";
}
else if (error.code == kCLErrorGeocodeFoundPartialResult)
{
self.address.text=@"Partial geocode result";
}
else
{
self.address.text =
[NSString stringWithFormat:@"Unknown error: %@",
error.description];
}
}
];
答案 0 :(得分:0)
首先通过检查是否为零来尝试处理错误:
if (error) {
// handle here
NSLog(@"error: %@", error);
return; // exit statement
}
// handle code here if there was no error
并且在调用对象时使用此实例,避免strong reference cycles创建自己的弱对象。
__weak typeof(self) weakSelf = self;
因此self.address.text=@"Partial geocode result";
将转向weakSelf.address.text=@"some text here...";