如何从经纬度获取地名?

How to get place name from lattitude and longitude?

我正在从 api 获取纬度和经度,并且需要从这些坐标获取地名以便我使用此代码块

CLGeocoder *ceo = [[CLGeocoder alloc]init];

CLLocation *LocationAtual=[[CLLocation alloc] initWithLatitude:[[latlong objectForKey:@"latitude"] doubleValue] 
                                                     longitude:[[latlong objectForKey:@"longitude"] doubleValue]];

NSLog(@"loc %@", LocationAtual);

[ceo reverseGeocodeLocation:LocationAtual  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);

  NSLog(@"I am currently at %@",locatedAt);
  NSLog(@"  ");
}
];

但是当我控制这条线时

 [ceo reverseGeocodeLocation:LocationAtual  completionHandler:^(NSArray *placemarks, NSError *error)  

此代码块未执行。控制开始。

尝试使用 This Question 的 Ram G 答案中的这段代码,它适合你

- (void) getAddressFromLatLon:(CLLocation *)bestLocation
{
    NSLog(@"%f %f", bestLocation.coordinate.latitude, bestLocation.coordinate.longitude);
    CLGeocoder *geocoder = [[CLGeocoder alloc] init] ;
    [geocoder reverseGeocodeLocation:bestLocation
                   completionHandler:^(NSArray *placemarks, NSError *error)
    {
        if (error){
            NSLog(@"Geocode failed with error: %@", error);
            return;
        }
        CLPlacemark *placemark = [placemarks objectAtIndex:0];
        NSLog(@"placemark.ISOcountryCode %@",placemark.ISOcountryCode);
        NSLog(@"locality %@",placemark.locality);
        NSLog(@"postalCode %@",placemark.postalCode);

    }];

}

在你的 class 或任何其他 class 的 viewDidLoad 中只需添加:

- (void)viewDidLoad
{
    [super viewDidLoad];
// Add observer
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gotPlaceInfo:) name:@"gotPlaceInfoNotification" object:nil];
}

//define `gotPlaceInfo`:
-(void)gotPlaceInfo:(NSNotification *)notif{

NSLog(@"place mark dict is  %@", notif.userInfo);
}

我刚刚在您的代码中添加了一行:

    CLGeocoder *ceo = [[CLGeocoder alloc]init];

    CLLocation *LocationAtual=[[CLLocation alloc] initWithLatitude:[[latlong objectForKey:@"latitude"] doubleValue] 
                                                         longitude:[[latlong objectForKey:@"longitude"] doubleValue]];

    NSLog(@"loc %@", LocationAtual);

    [ceo reverseGeocodeLocation:LocationAtual  completionHandler:^(NSArray *placemarks, NSError *error)
    {
      CLPlacemark *placemark = [placemarks objectAtIndex:0];

NSDictionary *currentLocationDictionary = @{@"CLPlacemark":CLPlacemark};
      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);

      NSLog(@"I am currently at %@",locatedAt);
      NSLog(@"  ");
NSNotification *notification2 = [NSNotification notificationWithName:@"gotPlaceInfoNotification" object:self userInfo:currentLocationDictionary];
    [[NSNotificationCenter defaultCenter] postNotification:notification2];
    }
    ];

希望能给大家一个整体的学习