2012-12-13 26 views
5

मैं उद्देश्य-सी प्रोग्रामिंग के लिए नया हूं।आईओएस में CLGeoCoder का उपयोग करके ज़िप कोड/पोस्टल कोड का उपयोग करके अक्षांश देशांतर कैसे प्राप्त करें?

मैं ज़िप कोड/पोस्टल कोड द्वारा MapView में स्थान प्रदर्शित करना चाहता हूं। इसके लिए मुझे अक्षांश और देशांतर खोजना है।

मैंने पहले ही वेब-सेवा में ज़िप कोड को पास करके और सभी जानकारी के साथ प्रतिक्रिया प्राप्त करके कई समाधानों का उल्लेख किया था।

लेकिन मैं किसी भी वेब सेवा का उपयोग नहीं करना चाहता हूं। मैं इसे CLGeocoder

का उपयोग करके करना चाहता हूं, मैं अक्षांश देशांतर खोजने के लिए नीचे कोड का उपयोग कर रहा हूं।

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

यह कोड काम कर रहा है अगर मैं शहर का नाम, देश का नाम या सड़क नाम पारित कर रहा हूं लेकिन ज़िप कोड को पार करते समय काम नहीं कर रहा हूं।

अग्रिम धन्यवाद।

उत्तर

5
-(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 { 

    } 
    }]; 

} 

मुझे पता है कि यह काम कर रहा है या नहीं तो कृपया ...

मुबारक कोडिंग !!!

+0

मदद के लिए धन्यवाद ... लेकिन मैंने पहले ही यह किया है .... –

+0

धन्यवाद निर्वापटेल। यह मेरे लिए काम करता है। –

+0

मैंने इस कोड का जवाब [इस प्रश्न का उत्तर] (http://stackoverflow.com/a/33460514/981049) में किया है, और अक्षांश/देशांतर प्लेसमार्क शब्दकोश/ऑब्जेक्ट में वापस पाया जा सकता है। –