Florian Schaal
Florian Schaal

Reputation: 2660

Getting zipcode into a textfield

So I looked at the sample code by Apple for getting the users location. This works fine for me and the location its display is correct.

Now I want to display the zipcode in a textfield called "zipCode" that I have created. How would I do that?

tryed:

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations {
    CLLocation *location = [locations objectAtIndex:0];
    NSLog(@"lat%f - lon%f", location.coordinate.latitude, location.coordinate.longitude);
    zipCode.text = self.placemark.postalCode;
}

But that didn't work.

Upvotes: 0

Views: 133

Answers (2)

Roland Keesom
Roland Keesom

Reputation: 8298

You need to use reverseGeocodeLocation, like:

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations {

    CLLocation *location = [locations objectAtIndex:0];
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:location completionHandler:
     ^(NSArray* placemarks, NSError* error){
         if ([placemarks count] > 0)
         {
             CLPlacemark *placemark = [placemarks objectAtIndex:0];
             zipCode.text = placemark.postalCode;
         }
     }];
}

Remember to import the CoreLocation.framework

Upvotes: 2

Mat
Mat

Reputation: 7643

You have to use the Reverse Geocoding if you want covert informations about a location on the map.
It's all written here.

Upvotes: 0

Related Questions