user3927993
user3927993

Reputation: 15

Geocoder to fill UITextField

I have an app that fills in the user location into a textfield, if the user allows location sharing. Everything works but my question is how do I get the results to be split up into multiple text fields.

Currently the "address" text field displays everything. All I want in that field is the street address, and another one for City, State, Zip, etc. Any suggestions would be appreciated! I've looked at the documentation for example for zip code I've tried

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

but that doesn't work

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

Upvotes: 0

Views: 209

Answers (2)

BHASKAR
BHASKAR

Reputation: 1201

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

Upvotes: 0

lihudi
lihudi

Reputation: 865

You should check the reference page for CLPlacemark class.

There you will find the properties you need: postal code, locality ...

Upvotes: 0

Related Questions