简体   繁体   English

地理编码到UITextfield不会显示

[英]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.

相关问题 UITextField不显示NSFontAttributes - UITextField not displaying NSFontAttributes 正确显示没有UITextField的UIDatePicker - Properly displaying UIDatePicker without UITextField 在UITextfield中显示来自HTML代码的文本 - Displaying text from HTML code inside a `UITextfield` 在UITextField中显示右视图和清除按钮 - Displaying both a right view and a clear button in UITextField UITableViewController中的customCell UITextField在运行时快速期间未显示 - customCell UITextField in UITableViewController not displaying during runtime swift iOS:选择时显示从UITableView到UItextField的选定项目 - IOS: displaying selected items from a UITableView to a UItextField when select 单击uitextfield时调用uitableview-数据未显示-iOS - Calling uitableview while clicking uitextfield-Data not displaying -iOS UITextField切断了文本的右侧,并且没有在正确的位置显示光标 - UITextField is cutting off the right side of text and not displaying the cursor at the correct position 在 iOS UITextField 上显示验证错误类似于 Android 的 TextView.setError() - Displaying validation error on iOS UITextField similar to Android's TextView.setError() UITextField如何使.text在右侧键入数字时始终显示&#39;$&#39;符号? - UITextField how to make the .text always displaying '$' sign while typing numbers on the right side?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM