[英]Geocoding to UITextfield not displaying
I am using reverse geocoding which works but I am trying to separate the attributes. 我使用的是反向地理编码,但我试图分离属性。 For example I want to have zip code displayed in it's own text field. 例如,我想在其自己的文本字段中显示邮政编码。
This is what I've tried but I get a breakpoint at if([placemarks count] >0) 这是我尝试过的方法,但是在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];
}
}
];
Try to handle error first by checking if its nil: 尝试首先通过检查错误是否为零来处理错误:
if (error) {
// handle here
NSLog(@"error: %@", error);
return; // exit statement
}
// handle code here if there was no error
and avoid strong reference cycles create a weak object of your self, when calling a object use this instnace. 并且在调用对象使用此实例时,避免强引用循环会创建您自己的弱对象。
__weak typeof(self) weakSelf = self;
so self.address.text=@"Partial geocode result";
因此self.address.text=@"Partial geocode result";
will turn to weakSelf.address.text=@"some text here...";
会变成weakSelf.address.text=@"some text here...";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.