CLGeocoder
//正向地理编码
-(void)geocoder
{
CLGeocoder *coder = [[CLGeocoder alloc] init];
[coder geocodeAddressString:@"深圳大学" completionHandler:^(NSArray *placemarks, NSError * _Nullable error) {
CLPlacemark *placeMark = placemarks[0];
//经纬度
NSLog(@"%lf %lf",placeMark.location.coordinate.latitude,placeMark.location.coordinate.longitude);
//位置信息
//NSLog(@"%@",placeMark.addressDictionary);
for (NSString *key in placeMark.addressDictionary) {
NSLog(@"%@",placeMark.addressDictionary[key]);
if([placeMark.addressDictionary[key] isKindOfClass:[NSArray class]])
{
for (NSString *string in placeMark.addressDictionary[key]) {
NSLog(@"1111%@",string);
}
}
}
//名字
NSLog(@"%@",placeMark.name);
//街道
NSLog(@"%@",placeMark.thoroughfare);
//城市
NSLog(@"%@",placeMark.locality);
//区
NSLog(@"%@",placeMark.subLocality);
//省
NSLog(@"%@",placeMark.administrativeArea);
NSLog(@"%@",placeMark.subAdministrativeArea);
}];
}
//反向地理编码
(void)reverseGeoCoder
{
CLGeocoder *coder = [[CLGeocoder alloc] init];
CLLocation *location = [[CLLocation alloc] initWithLatitude:22.533910 longitude:113.936272];
[coder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
for (CLPlacemark *placeMark in placemarks) {
NSLog(@"%@",placeMark.name);
NSLog(@"%@",placeMark.thoroughfare);
NSLog(@"%@",placeMark.locality);
NSLog(@"%@",placeMark.subLocality);
NSLog(@"%@",placeMark.administrativeArea);
NSLog(@"%@",placeMark.subAdministrativeArea);
}
}];
}