2016-03-25 8 views
0

Ich bin in der Lage, tatsächlichen Ort in AutoFill Suche Textfeld zu finden. Aber ich kann keine Postleitzahl aus diesem Code finden. Bitte gib mir eine Lösung.So erhalten Sie Postleitzahl vom aktuellen Standort in iOS mit Google API

-(void)googleAPICallWithText:(NSString*)strData { 


strData = [strData stringByReplacingOccurrencesOfString:@" " withString:@"+"]; 
NSLog(@"Value of allSelectedItems = %@", strData); 


//google api url 
NSString *web_service_URL = [NSString stringWithFormat:@"%@json?input=%@&sensor=%@&key=%@",GOOGLE_GEOLOCATION_URL,strData,@"true",GOOGLE_API_KEY]; 

NSMutableString *reqData = [NSMutableString stringWithFormat:@""]; 
    id jsonDta= [reqData dataUsingEncoding:NSUTF8StringEncoding]; 

NSString *jsonRepresantationFormat = [[NSString alloc]initWithData:jsonDta encoding:NSUTF8StringEncoding]; 
[self WebsewrviceAPICall:web_service_URL webserviceBodyInfo:jsonRepresantationFormat]; 
} 

-(void)WebsewrviceAPICall:(NSString *)serviceURL webserviceBodyInfo:(NSString *)bodyString { 
self.connectionTimer = [NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:@selector(cancelInsingAPICall) userInfo:nil repeats:NO]; 

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:serviceURL]]; 
[theRequest setHTTPMethod:@"POST"]; 
[theRequest setHTTPBody:[bodyString dataUsingEncoding:NSUTF8StringEncoding]]; 

self.connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; 

if(self.connection){ 
    self.receivedData = [NSMutableData data]; 

} 
} 

Antwort

0

Wenn Ihre Anforderungen nicht auf die Google-API beschränkt sind, können Sie stattdessen CoreLocation verwenden. Wenn Sie den Standort im Rückruf des Stellvertreters abrufen, können Sie das Standortobjekt in eine Ortsmarke, die die Postleitzahl enthält, umkodieren.

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

    CLGeocoder *geocoder = [[CLGeocoder alloc] init]; 
    [geocoder reverseGeocodeLocation:current completionHandler:^(NSArray *placemarks, NSError *error) 
    { 
     if (!error) { 
      CLPlacemark *placemark = [placemarks objectAtIndex:0]; 
      NSString *zip = placemark.postalCode; 

      // do stuff with zip code 
     } else { 
      // geocode failed 
     } 
    }]; 
} 
Verwandte Themen