- (NSString *)getZipCode:(NSString *)address
{
NSString *zipCode = @"";
if (address.length>0) {
NSString *urlString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@&sensor=false",[self encodeUrlString:address]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10];
[request setHTTPMethod: @"GET"];
NSError *requestError;
NSURLResponse *urlResponse = nil;
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&requestError];
NSError *error;
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:response options:kNilOptions error:&error];
for (NSDictionary *dic in [result valueForKeyPath:@"results.address_components"][0]) {
if ([[dic valueForKey:@"types"][0] isEqualToString:@"postal_code"]) {
zipcode = [dic valueForKey:@"short_name"];
}
}
return zipCode;
}