从reverseGeocodeLocation

时间:2016-12-19 06:58:02

标签: ios objective-c clgeocoder

我想使用reverseGeocodeLocation查找照片位置。我使用以下功能

@interface GalleryPhotosViewController ()
{
CLPlacemark *placemark;
}
@end

-(NSString *)getLocation:(CLLocation *)locations{
CLGeocoder *geocoder = [CLGeocoder new];
[geocoder reverseGeocodeLocation:locations completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
    placemark = [placemarks lastObject];
}];
return placemark.name;
}

调用函数时我无法获得该名称,但在执行其他代码部分后我得到了该名称。我知道reverseGeocodeLocation有一个完成块,它在执行到达时被移交给另一个线程。但是在调用函数时我需要获得该名称。我经历了很多解决方案,但无法解决我的问题。 我想在currentLocation中接收位置名称。

CLLocation *loc = asset.location;
NSString *currentLocation = [self getLocation:loc];

我应该在哪里更改代码。请帮忙。

3 个答案:

答案 0 :(得分:1)

这是因为您在reverseGeocodeLocation的完成处理程序中获取值之前返回值。现在,您无法从任何completion block返回值,因此您需要创建自己的completionblock并在获得结果时调用它,

 -(void)getLocation:(CLLocation *)locations withcompletionHandler : (void(^)(NSString *name))completionHandler{


CLGeocoder *geocoder = [CLGeocoder new];
[geocoder reverseGeocodeLocation:locations completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
    placemark = [placemarks lastObject];

    completionHandler(placemark.name);

}];

}

并称之为,

   [self  getLocation:location withcompletionHandler:^(NSString *name) {

    NSLog(@"your name : %@",name);  // or do whatever you want with name!
}];

更新:

如果你想在注释中提到完整处理程序中的整个数组,那么你的方法应该是,

 -(void)getLocation:(CLLocation *)locations withcompletionHandler : (void(^)(NSArray *arr))completionHandler{



CLGeocoder *geocoder = [CLGeocoder new];
[geocoder reverseGeocodeLocation:locations completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks,
                                                               NSError * _Nullable error) {
   // placemark = [placemarks lastObject];

    completionHandler(placemarks);

}];

}

你可以称之为,

 [self getLocation:location withcompletionHandler:^(NSArray *arr) {

    // you will get whole array here in `arr`, now you can manipulate it according to requirement
    NSLog(@"your response array : %@",arr);

    CLPlacemark *placemark = [arr lastObject];

}];

答案 1 :(得分:0)

reverseGeocodeLocation的方法CLGeocoder以异步方式运行,因此您不会将位置数据作为被调用函数的返回值。它使用完成处理程序返回位置数据,您可以使用该完成处理程序块来更新UI或进一步处理您的位置数据。

答案 2 :(得分:-1)

  

获取您必须经过纬度和经度的城市,州和地址,   通过纬度和经度将传递给地理编码器块和   在块内你将放置标记数组。

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

    CLLocation *myLocation = [[CLLocation alloc]initWithLatitude:23.0225
                                                        longitude:72.5714];



    [geocoder reverseGeocodeLocation:myLocation
                   completionHandler:^(NSArray *placemarks, NSError *error) {

                       if (error) {
                           NSLog(@"Geocode failed with error: %@", error);
                           return;
                       }

                       if (placemarks && placemarks.count > 0)
                       {
                           CLPlacemark *placemark = placemarks[0];

                           NSDictionary *addressDictionary =
                           placemark.addressDictionary;

                           NSLog(@"%@ ", addressDictionary);
                           NSString *address = [addressDictionary
                                                objectForKey:(NSString *)kABPersonAddressStreetKey];
                           NSString *city = [addressDictionary
                                             objectForKey:(NSString *)kABPersonAddressCityKey];
                           NSString *state = [addressDictionary
                                              objectForKey:(NSString *)kABPersonAddressStateKey];


                           NSLog(@"%@ %@ %@ %@", address,city, state);
                       }

                   }];