如何使用当前位置坐标获取州(国家)信息?

时间:2017-02-17 06:43:25

标签: ios objective-c location core-location

如何从位置坐标获取有关状态的信息?是否有任何第三方框架或API从lat-long值提供州/省?

3 个答案:

答案 0 :(得分:2)

在下面的方法中传递更新的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);
    }];
}

答案 1 :(得分: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];
          }

 ];
}

答案 2 :(得分:1)

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

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://地标字典包含用户的其他信息,如下所示。

@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