【问题标题】:How to get zip code from current location in iOS using google api如何使用 google api 从 iOS 中的当前位置获取邮政编码
【发布时间】:2016-03-25 13:41:06
【问题描述】:

我能够在自动填充搜索文本字段中找到实际位置。但我无法从此代码中找到邮政编码。请给我一个解决方案。

-(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];

}
}

【问题讨论】:

    标签: ios objective-c google-api google-places-api zipcode


    【解决方案1】:

    如果您的要求不限于使用 Google 的 API,您可以改用 CoreLocation。当您在委托的回调中获取位置时,您可以将位置对象反向地理编码为包含邮政编码的地标。

    - (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
             }
         }];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-05
      • 1970-01-01
      • 2014-09-26
      • 2015-04-12
      • 1970-01-01
      相关资源
      最近更新 更多