Reputation: 2660
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
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