繁体   English   中英

如何在iOs中使用CLGeoCoder使用邮政编码/邮政编码获得纬度经度?

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

我是Objective-C编程的新手。

我想通过邮政编码/邮政编码在MapView中显示位置。 为此,我必须找到纬度和经度。

我已经通过将zip代码传递给Web服务并获得所有信息的响应来引用了许多解决方案。

但我不想使用任何网络服务。 我想通过使用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";
        }
     }
  }];
}

如果我传递城市名称,国家名称或街道名称但在传递邮政编码时无法正常工作,则此代码正常工作。

提前致谢。

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

   }
  }];

 }

请让我知道它的工作与否......

快乐的编码!!!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM