【问题标题】:Geocoding to UITextfield not displaying地理编码到 UITextfield 不显示
【发布时间】:2014-08-15 09:33:12
【问题描述】:

我正在使用有效的反向地理编码,但我正在尝试分离属性。例如,我想在它自己的文本字段中显示邮政编码。

这是我尝试过的,但我在 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];
             }
         }
         ];

【问题讨论】:

    标签: ios objective-c uitextfield geocoding reverse-geocoding


    【解决方案1】:

    尝试首先通过检查它是否为 nil 来处理错误:

    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...";

    【讨论】:

      猜你喜欢
      • 2014-10-07
      • 1970-01-01
      • 2014-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多