【问题标题】:Geocoder to fill UITextField地理编码器填充 UITextField
【发布时间】:2014-10-07 22:55:19
【问题描述】:

如果用户允许位置共享,我有一个应用程序可以将用户位置填充到文本字段中。一切正常,但我的问题是如何将结果拆分为多个文本字段。

目前“地址”文本字段显示所有内容。我在该字段中想要的只是街道地址,以及城市、州、邮编等的另一个地址。任何建议将不胜感激!我查看了文档,例如我尝试过的邮政编码

    self.zip.text= 
    NSString *postalCode = [NSString stringWithFormat:@"%@",placemark.postalCode];

但这不起作用

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    if(error.code == kCLErrorDenied)
    {
        self.address.text = @"Location information denied";
    }
}

- (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];

             }
             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];
             }
         }
         ];

        //Stop updating location until they click the button again
        [manager stopUpdatingLocation];
    }
}

【问题讨论】:

  • 所以在您的代码中,foundPlacemark.description 是您要拆分的字符串的位置?

标签: ios objective-c ipad nsstring geocoding


【解决方案1】:

您应该检查 CLPlacemark 类的 reference page

在那里您会找到您需要的属性:邮政编码、地区...

【讨论】:

  • 做到了,但是当我添加它时,基本上@bhaskar 在它下面发布的内容不会显示
【解决方案2】:
CLPlacemark *foundPlacemark = [placemarks lastObject];
NSString *subThoroughfare = [NSString stringWithFormat:@"%@",placemark.subThoroughfare];
NSString *thoroughfare = [NSString stringWithFormat:@"%@",placemark.thoroughfare];
NSString *postalCode = [NSString stringWithFormat:@"%@",placemark.postalCode];
NSString *country = [NSString stringWithFormat:@"%@",placemark.country];
NSString *locality = [NSString stringWithFormat:@"%@",placemark.locality];
NSString *administrativeArea = [NSString stringWithFormat:@"%@",placemark.administrativeArea];

【讨论】:

  • 所以我试过这个,但我得到错误属性 postalCode not found on object of type NSArray
  • 然后你输入一个检查邮政编码是否存在然后它加载
  • @user3927993 错误:“在 NSArray 类型的对象上找不到邮政编码”意味着您正在尝试从地标数组(NSArray* 地标)中获取邮政编码,而不是仅从地标(CLPlacemark *foundPlacemark = [placemarks objectAtIndex:0])。
猜你喜欢
  • 1970-01-01
  • 2018-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-05
相关资源
最近更新 更多