2015-09-24 4 views
0

Я пытаюсь получить ZipCode текущего местоположения пользователей. Это хорошо работает в США, но в других странах есть некоторые проблемы.Zipcode не отображается в обратномGeocode

if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { 

     [self.locationManager requestAlwaysAuthorization]; 
    } 

    _coder = [[CLGeocoder alloc] init]; 
    _locationManager = [[CLLocationManager alloc]init]; 
    _locationManager.delegate = self; 
    [_locationManager startUpdatingLocation]; 

     //Block address 
    [_coder reverseGeocodeLocation: _locationManager.location completionHandler: 
    ^(NSArray *placemarks, NSError *error) { 

      //Get address 
     CLPlacemark *placemark = [placemarks objectAtIndex:0]; 
     if(placemark.addressDictionary == NULL){ 
       NSLog(@"Error in getting address"); 
     }else{ 
NSLog(@"Placemark array: %@",placemark.addressDictionary); 
NSString *locateaddress; 
       //String to address 
if(self.switcher.on){ 
      locateaddress = [placemark.addressDictionary valueForKey:@"ZIP"]; 
if (!locateaddress || [locateaddress isKindOfClass:[NSNull class]]){ 

      } 
      }else{ 
       locateaddress = zipCodeString; 
      } 

Линия NSLog (@ "Placemark массив ...) возвращает это ..

> Placemark array: { 
>  City = Jerusalem; 
>  Country = Israel; 
>  CountryCode = IL; 
>  FormattedAddressLines =  (
>   "to begin south", 
>   Jerusalem, 
>   Israel 
> ); 
>  Name = "to begin south"; 
>  State = Jerusalem; 
>  Street = "to begin south"; 
>  Thoroughfare = "to begin south"; } 

Так 1) Почему там нет почтового индекса или параметр Почтового индекса 2) Как я могу получить ZIP/Почтовый индекс из этой информации?

+0

почему вы получаете адрес автофокус ter startUpdatingLocation? означает, что вам нужно будет получить адрес после получения метода делегирования, если какое-либо местоположение найдено. –

+0

Это то, что вызывает проблему Zip? Или это то, что я должен исправить? – user2738258

+0

Я думаю, что это основная проблема не получения zipcode. см. мой ответ для более подробной информации. –

ответ

0

Попробуйте это. может быть поможет вам.

//Get Location 
-(void)updateLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    self.currentCLLocation = newLocation; 
    [self getCurrentAddress]; 
} 

//Get address using location. 
-(void)getCurrentAddress 
{ 

    //Geocoding Block 
    CLGeocoder *geoCoder = [[CLGeocoder alloc] init]; 
    CLLocation *currentLocation = [[CLLocation alloc] initWithLatitude:[self.sharedLat doubleValue] longitude:[self.sharedLong doubleValue]]; 
    [geoCoder reverseGeocodeLocation: currentLocation completionHandler: 
    ^(NSArray *placemarks, NSError *error) 
    { 
    //Get nearby address 
    self.placemark = [placemarks objectAtIndex:0]; 

    //String to hold address 
    NSString *locatedAt = [[self.placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "]; 
    // NSLog(@"City = %@",placemark.locality); 
    // NSLog(@"Country = %@",placemark.country); 

    self.currentCity = self.placemark.locality; 
    self.currentCountry = self.placemark.country; 

    //Print the location to console 
    NSLog(@"I am currently at %@ City",self.currentCity); 
    }]; 
} 
+0

Не работает для меня в Иерусалиме, Израиль – user2738258