2012-12-13 77 views
5

我是Objective-C編程的新手。如何使用郵政編碼/郵政編碼在iOs中使用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

感謝NiravPatel。這對我有用。 –

+0

我已在[對此問題的回答](http://stackoverflow.com/a/33460514/981049)中對此代碼進行了更新,並且可以在返回的地標詞典/對象中找到緯度/經度。 –

相關問題