2011-04-30 40 views
5

如何使用mapkit獲取當前位置的郵政編碼,我沒有找到任何用於在文檔中獲取此API的API。我使用了CLLocationManager的座標,取樣,水平,垂直,球場和速度參數,但未能獲得郵政編碼。獲取當前位置的郵政編碼 - iPhone SDK

任何人都可以給我API或示例代碼來完成它。

是否有可能使用iphone中的當前位置獲取郵政編碼?

回答

1

您可以使用緯度和經度創建一個MKPlacemark object,其中包括郵政編碼。

Here是一個示例,顯示如何做到這一點。

+0

非常感謝尼克,我測試了它的工作,感謝你的幫助。 – 2011-04-30 11:15:03

9

反向地理編碼在iPhone:

首先添加<MobileCoreServices/MobileCoreServices.h>框架。

-(void)CurrentLocationIdentifier 
    { 
     //---- For getting current gps location 
     CLLocationManager *locationManager; 
     CLLocation *currentLocation; 

     locationManager = [CLLocationManager new]; 
     locationManager.delegate = self; 
     locationManager.distanceFilter = kCLDistanceFilterNone; 
     locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
     [locationManager startUpdatingLocation]; 
    } 

使用GPS位置獲取地點詳情的反向地理編碼。

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 
{ 
    currentLocation = [locations objectAtIndex:0]; 
    [locationManager stopUpdatingLocation]; 

    CLGeocoder *geocoder = [[CLGeocoder alloc] init] ; 
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) 
    { 
     if (!(error)) 
     { 
      CLPlacemark *placemark = [placemarks objectAtIndex:0]; 
      NSLog(@"\nCurrent Location Detected\n"); 
      NSLog(@"placemark %@",placemark); 
      NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "]; 

      NSString *Address = [[NSString alloc]initWithString:locatedAt]; 
      NSString *Zipcode = [[NSString alloc]initWithString:placemark.postalCode]; 
      NSLog(@"%@",Zipcode);  
     } 
     else 
     { 
      NSLog(@"Geocode failed with error %@", error); // Error handling must required 
     } 
    }]; 
} 

有關詳細信息,從GPS獲得:

placemark.region 
placemark.country 
placemark.locality 
placemark.name 
placemark.ocean 
placemark.postalCode 
placemark.subLocality 
placemark.location