【发布时间】:2017-07-06 13:49:06
【问题描述】:
如何从位置坐标中获取有关状态的信息?是否有任何第三方框架或 API 可以根据经纬度值提供州/省?
【问题讨论】:
标签: ios objective-c location core-location
如何从位置坐标中获取有关状态的信息?是否有任何第三方框架或 API 可以根据经纬度值提供州/省?
【问题讨论】:
标签: ios objective-c location core-location
在下面的方法中传递更新的 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);
}];
}
【讨论】:
可以使用 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];
}
];
}
【讨论】:
您可以使用反向地理代码获取它。见下例:
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
【讨论】: