【问题标题】:how to get city name via CLlocationManager using latitude and longitude in IOS?如何使用 IOS 中的纬度和经度通过 CLlocationManager 获取城市名称?
【发布时间】:2014-07-22 13:20:07
【问题描述】:

我正在尝试使用纬度和经度通过 CLLocationManager 获取地址,但它只返回州和国家/地区名称,我还想要城市,我正在附上我的代码有人可以告诉我如何更改我的代码以获取城市名称.下面是我的代码

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    currentLocation = [locations objectAtIndex:0];
    CLGeocoder *geocoder1 = [[CLGeocoder alloc] init];

     NSLog(@"Detected Location : %f, %f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);

    //CLLocation *newLocation = [[CLLocation alloc]initWithLatitude:26.9260 longitude:75.8235];

    CLLocation *newLocation = [[CLLocation alloc]initWithLatitude:currentLocation.coordinate.latitude longitude:currentLocation.coordinate.longitude];

    [geocoder1 reverseGeocodeLocation:newLocation
                   completionHandler:^(NSArray *placemarks, NSError *error) {

                       if (error) {
                           NSLog(@"Geocode failed with error: %@", error);
                           return;
                       }

                       if (placemarks && placemarks.count > 0)
                       {
                           CLPlacemark *placemark = placemarks[0];

                           NSDictionary *addressDictionary =
                           placemark.addressDictionary;

                           NSLog(@"%@ ", addressDictionary);
                           NSString *address = [addressDictionary
                                                objectForKey:(NSString *)kABPersonAddressStreetKey];
                           NSString *city = [addressDictionary
                                             objectForKey:(NSString *)kABPersonAddressCityKey];
                           NSString *state = [addressDictionary
                                              objectForKey:(NSString *)kABPersonAddressStateKey];
                           NSString *zip = [addressDictionary
                                            objectForKey:(NSString *)kABPersonAddressZIPKey];


                           NSLog(@"%@ %@ %@ %@", address,city, state, zip);
                       }

                   }];
}

【问题讨论】:

  • 您并没有真正获得“通过 CLLocationManager 的地址”。您通过 CLGeocoder 获得它(CLLocationManager 只是给您坐标)。代码看起来不错。它可能特定于 CLGeocoder(Apple)可能无法很好地进行地理编码的坐标(Google 似乎在这方面做得更好)。坐标是 26.9260、75.8235 吗?

标签: ios cllocationmanager city


【解决方案1】:

试试这个..

-(NSString*)getAddressFromLatLong : (NSString *)latLng {
    //  NSString *string = [[Address.text stringByAppendingFormat:@"+%@",cityTxt.text] stringByAppendingFormat:@"+%@",addressText];
    NSString *esc_addr =  [latLng stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr];
        NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
        NSMutableDictionary *data = [NSJSONSerialization JSONObjectWithData:[result dataUsingEncoding:NSUTF8StringEncoding]options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *dataArray = (NSMutableArray *)[data valueForKey:@"results" ];
        if (dataArray.count == 0) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Please Enter a valid address" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [alert show];
        }else{
            for (id firstTime in dataArray) {
                NSString *jsonStr1 = [firstTime valueForKey:@"formatted_address"];
                return jsonStr1;
            }
        }

    return nil;
}

发送逗号分隔的纬度经度..并尝试它工作正常

参考链接Get Address From Latitude Longitude using Apple Function in iOS & iPad

【讨论】:

  • 我不想使用 google map API ,有没有办法使用 CLLocationManager 获取城市名称。
  • 在上一个项目中我使用了这个.,, 但我不关心 CLLocationManager...请尝试搜索,如果可用的话,那么您肯定会得到...尝试搜索更多,。 ,..
【解决方案2】:

您正在使用来自 AddressBook 框架的 ABPerson 类的 k-constants 用于 CLGeocoder 对象键。虽然有些可能匹配,但我很确定其他人不会。

你可以试试……

NSMutableArray *addrArray = [[NSMutableArray alloc] init];
//to return address line(s)
addrArray = [NSMutableArray arrayWithArray: [p.addressDictionary objectForKey:@"FormattedAddressLines"]];
[addrArray removeLastObject]; //country name
NSString *addressString = [addrArray componentsJoinedByString:@" ,"];
#if DEBUG
                NSLog(@"Address: %@", addressString);
#endif

【讨论】:

    【解决方案3】:

    我们将根据经纬度得到完整的地址。这里是示例代码。

     - (void) getAddressFromLatLon:(CLLocation *)location
     {
    
          CLGeocoder *geocoder = [[CLGeocoder alloc] init] ;
          [geocoder reverseGeocodeLocation:location
                   completionHandler:^(NSArray *array, NSError *error){
        if (error){
            NSLog(@"Geocode failed with error: %@", error);
            return;
        }
        CLPlacemark *placemark = [array objectAtIndex:0];
         NSLog(@"City %@",placemark.subAdministrativeArea);
         NSLog(@"State %@",placemark.administrativeArea);
    
         }];
     }
    

    在下面我提到了不同的类型。很容易你可以检查它。这些都属于CLPlacemark类对象。这个类可以在CoreLocation框架上使用。

    @property (nonatomic, readonly, copy, nullable) NSString *name; // eg. Apple Inc.
    @property (nonatomic, readonly, copy, nullable) NSString *thoroughfare; // street name, eg. Infinite Loop
    @property (nonatomic, readonly, copy, nullable) NSString *subThoroughfare; // eg. 1
    @property (nonatomic, readonly, copy, nullable) NSString *locality; // city, eg. Cupertino
    @property (nonatomic, readonly, copy, nullable) NSString *subLocality // neighborhood, common name, eg. Mission District
    @property (nonatomic, readonly, copy, nullable) NSString *administrativeArea; // state, eg. CA
    @property (nonatomic, readonly, copy, nullable) NSString *subAdministrativeArea; // City, eg. Santa Clara
    @property (nonatomic, readonly, copy, nullable) NSString *postalCode; // zip code, eg. 95014
    @property (nonatomic, readonly, copy, nullable) NSString *ISOcountryCode; // eg. US
    @property (nonatomic, readonly, copy, nullable) NSString *country; // eg. United States
    @property (nonatomic, readonly, copy, nullable) NSString *inlandWater; // eg. Lake Tahoe
    @property (nonatomic, readonly, copy, nullable) NSString *ocean; // eg. Pacific Ocean
    @property (nonatomic, readonly, copy, nullable) NSArray<NSString *> *areasOfInterest;
    

    你想要什么,你只需添加它并得到结果。这里是参考link

    【讨论】:

      猜你喜欢
      • 2014-04-14
      • 1970-01-01
      • 2013-12-09
      • 1970-01-01
      • 2017-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多