How to get latitude longitude using Zip code/ Postal Code using CLGeoCoder in iOs?

clgeocoder, ios, iphone, location, objective-c

Solution

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:theSearchBar.text completionHandler:^(NSArray *placemarks, NSError *error) {
   //Error checking

   CLPlacemark *placemark = [placemarks objectAtIndex:0];
   MKCoordinateRegion region;
   region.center.latitude = placemark.region.center.latitude;
   region.center.longitude = placemark.region.center.longitude;
   MKCoordinateSpan span;
   double radius = placemark.region.radius / 1000; // convert to km

   //NSLog(@"Radius is %f", radius);
   span.latitudeDelta = radius / 112.0;

   region.span = span;
   mapView.showsUserLocation=TRUE;
   mapView.delegate = self;
   DisplayMap *ann = [[[DisplayMap alloc] init] autorelease]; 
   ann.title =theSearchBar.text; 
   ann.coordinate = region.center;
   ann.takeid = 0;
   [mapView addAnnotation:ann];



   [mapView setRegion:region animated:YES];
 }];

[theSearchBar resignFirstResponder];

}

-(void)GetCityName {

 CLGeocoder *geocoder = [[[CLGeocoder alloc] init] autorelease];

 CLLocation *currentLocation = [[[CLLocation alloc] initWithLatitude:dblLatitude longitude:dblLongitude] autorelease];

 [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
   if ([placemarks count] > 0) {
       CLPlacemark *placemark = [placemarks objectAtIndex:0];
       //addressString1 = [placemark locality];
       addressString1 = [placemark thoroughfare];
       addressString1 = [addressString1 stringByAppendingString:[NSString stringWithFormatAngry" %@",[placemark postalCode]]];//enter here your postal code...

       //NSLog(@"Address is : %@",addressString1);
       LblLocation.text = addressString1;

   }
   else {

   }
  }];

 }

please let me know it is working or not...

Happy Coding!!!

Problem

I'm new to Objective-C programming. I want to display location in MapView by Zip code / Postal Code. For that i have to find the latitude and longitude. I had already referred many solutions by passing zip-code to web-service and getting response with all information. But i don't want to use any web-service. I want to do it by using CLGeocoder I'm using below code for finding latitude longitude. ``` -(IBAction)fetchCoordinates:(id)sender { if (!self.geocoder) { self.geocoder = [[CLGeocoder alloc] init]; } NSString *address = [NSString stringWithFormat:@"%@ %@ %@", self.streetField.text, self.cityField.text, self.countryField.text]; [self.geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) { NSLog(@"mark :: %@", placemarks); if ([placemarks count] > 0) { CLPlacemark *placemark = [placemarks objectAtIndex:0]; CLLocation *location = placemark.location; CLLocationCoordinate2D coordinate = location.coordinate; self.coordinatesLabel.text = [NSString stringWithFormat:@"%f, %f", coordinate.latitude, coordinate.longitude]; if ([placemark.areasOfInterest count] > 0) { self.nameLabel.text = [placemark.areasOfInterest objectAtIndex:0]; } else { self.nameLabel.text = @"No Area of Interest Was Found"; } } }]; } ``` This code is working if I'm passing City name, Country Name or Street Name but not working when passing zip-code. Thanks in advance.

Original source

Related problems