如何从反向地理编码目标C中获得州和城市

问题描述

在阅读之前,请不要将其标记为重复项,因为在将问题发布到此处之前,我已经尝试了许多解决方案。

我有经度和纬度,我正在尝试获取城市和州名,但是我对Google Api的响应感到困惑,哪一个用于城市,哪个用于州。 有人可以帮我吗

我尝试使用此代码获取城市和州。

CLLocation *locSource = [[CLLocation alloc] initWithCoordinate:CLLocationCoordinate2DMake(pdblLatitude,pdblLongitude)
                    altitude:-1
          horizontalAccuracy:200
            verticalAccuracy:200
                   timestamp:[NSDate date]];
CLGeocoder *geocoder=[[CLGeocoder alloc]init];

CLLocationCoordinate2D coord;
coord.longitude = pdblLatitude;
coord.latitude = pdblLongitude;
// or a one shot fill
     
[geocoder reverseGeocodeLocation:locSource completionHandler:^(NSArray *placemarks,NSError *error) {
    CLPlacemark *placemark = placemarks[0];
    NSDictionary *addressDictionary = [placemark addressDictionary];
    
    cityName = [addressDictionary objectForKey:@"City"];
    stateName = [addressDictionary objectForKey:@"State"];

    NSLog(@"-=====0=-==========%@",addressDictionary);
}];

但是当我输入“哈里亚纳邦”时,并没有提供正确的详细信息。

现在我尝试了第二种方法

[[GMSGeocoder geocoder] reverseGeocodeCoordinate:CLLocationCoordinate2DMake(pdblLatitude,pdblLongitude) completionHandler:^(GMSReverseGeocodeResponse* response,NSError* error) {
    NSLog(@"reverse geocoding results: %@ ----",[response results]);
    for(GMSAddress* addressObj in [response results]) {
        NSLog(@"reverse geocoding results: ^^^^^^^  %@ ^^^^^^^^^",addressObj);
        NSLog(@"thoroughfare=%@",addressObj.thoroughfare);
        NSLog(@"locality=%@",addressObj.locality);
        NSLog(@"subLocality=%@",addressObj.subLocality);
        NSLog(@"administrativeArea=%@",addressObj.administrativeArea);
        NSLog(@"postalCode=%@",addressObj.postalCode);
        NSLog(@"country=%@",addressObj.country);
        NSLog(@"lines=%@",addressObj.lines);
    }
}];

在这方面,我对于应该使用哪个城市以及哪个州作为州感到困惑。

为了获得经纬度,我正在使用此代码

#pragma Get Lat LNG by address string
-(CLLocationCoordinate2D) getLocationFromAddressstring: (Nsstring*) addressstr andplaceId:(Nsstring *)placeId{
    [ProgressHUD show:kSTRING_LOADING Interaction:NO];
    
    double latitude = 0,longitude = 0;
    Nsstring *esc_addr =  [addressstr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  
    Nsstring *req = [Nsstring stringWithFormat:@"https://maps.google.com/maps/api/geocode/json?sensor=false&address=%@&key=%@",esc_addr,CONST_GOOGLE_API_KEY];
    
    Nsstring *response = [Nsstring stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
    if (response) {
        
        Nsstring *jsonString = response;
        NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
        id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
                
        NSArray *arrayResult = [json objectForKey:@"results"];
        if ([arrayResult count]>0) {
            NSDictionary *dict = [arrayResult objectAtIndex:0];
            latitude = [[[[dict objectForKey:@"geometry"] objectForKey:@"location"] objectForKey:@"lat"] doubleValue];
           longitude =  [[[[dict objectForKey:@"geometry"] objectForKey:@"location"] objectForKey:@"lng"] doubleValue];
        }
    }

    CLLocationCoordinate2D center;
    center.latitude= latitude;
    center.longitude = longitude;
    
    [ProgressHUD dismiss];
    
    return center;
}

任何人都可以帮助我或建议我从Google Api获取城市和州值的正确密钥名称是什么?

谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)