【问题标题】:How to get State (of Country) information using Current Location Coordinates?如何使用当前位置坐标获取(国家)信息?
【发布时间】:2017-07-06 13:49:06
【问题描述】:

如何从位置坐标中获取有关状态的信息?是否有任何第三方框架或 API 可以根据经纬度值提供州/省?

【问题讨论】:

    标签: ios objective-c location core-location


    【解决方案1】:

    在下面的方法中传递更新的 CLLocation 值。您可以从 CLPlacemark

    获得各种信息
    -(void)getLocationDetails:(CLLocation *)location{
        CLGeocoder *reverseGeocoder = [[CLGeocoder alloc] init];
            [reverseGeocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
            CLPlacemark *myPlacemark = [placemarks objectAtIndex:0];
            NSString *countryCode = myPlacemark.ISOcountryCode;
            NSString *countryName = myPlacemark.country;
            NSString *cityName= myPlacemark.subAdministrativeArea;
            NSLog(@"My country code: %@ and countryName: %@ MyCity: %@", countryCode, countryName, cityName);
        }];
    }
    

    【讨论】:

    • 当我尝试 CLLocation location = [[CLLocation alloc] initWithLatitude:19.0760 longitude:72.8777];它给了我 *我的国家代码:IN 和 countryName:印度 MyCity:孟买
    • 给我你正在尝试的纬度经度值。
    【解决方案2】:

    可以使用 placemark.administrativeArea 获取状态。

    CLLocation *loc = [[CLLocation alloc]initWithLatitude:coordinate.latitude longitude:coordinate.longitude
                       ];
    [ceo reverseGeocodeLocation:loc
              completionHandler:^(NSArray *placemarks, NSError *error) {
                  CLPlacemark *placemark = [placemarks objectAtIndex:0];
                  NSLog(@"placemark %@",placemark);
                  //String to hold address
                  NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
                  NSLog(@"addressDictionary %@", placemark.addressDictionary);
    
                  NSLog(@"placemark %@",placemark.region);
                  NSLog(@"placemark %@",placemark.country);  // Give Country Name
                  NSLog(@"placemark %@",placemark.locality); // Extract the city name
                  NSLog(@"location %@",placemark.name);
                  NSLog(@"location %@",placemark.ocean);
                  NSLog(@"location %@",placemark.postalCode);
                  NSLog(@"location %@",placemark.subLocality);
    
                  NSLog(@"location %@",placemark.location);
                  //Print the location to console
                  NSLog(@"I am currently at %@",locatedAt);
    
                   NSLog(@"state name %@",placemark.administrativeArea);
                  _City.text=[placemark.addressDictionary objectForKey:@"City"];
                  [locationManager stopUpdatingLocation];
              }
    
     ];
    }
    

    【讨论】:

    • 但它只会添加缩写。怎样才能得到state的全名?
    【解决方案3】:

    您可以使用反向地理代码获取它。见下例:

    geocoder = [[CLGeocoder alloc] init];
    
    //newlocation is CLLocation object you need to pass user location there.
    
    [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
            if (error == nil && [placemarks count] > 0) {
                placemark = [placemarks lastObject];
                // placemark.administrativeArea is state of use
    
            } else {
                NSLog(@"%@", error.debugDescription);
            }
        }];
    

    从 CLPlacemark.h://Placemark 字典中包含用户的其他信息将如下所示。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-19
      • 2022-01-18
      • 2012-06-20
      • 1970-01-01
      • 2015-12-29
      • 2011-02-17
      相关资源
      最近更新 更多