-1

I am getting the latitude and longitude from the api, and need to get the place name from these cooridinates so that I use this block of code

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(@"  ");
}
];

But when my control goes on this line

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

this block of code is not executed. Control goes.

mort
  • 12,988
  • 14
  • 52
  • 97
sandeep tomar
  • 303
  • 1
  • 5
  • 17
  • Yes sir i have issue with this concept – sandeep tomar May 25 '16 at 06:51
  • yes sir i will read about it, but can you help me for now – sandeep tomar May 25 '16 at 06:53
  • Get it from here @sandeep http://stackoverflow.com/questions/14346516/set-address-string-with-reversegeocodelocation-and-return-from-method – Vizllx May 25 '16 at 06:55
  • @sandeeptomar Please print your LocationAtual value. – Dharmbir Singh May 25 '16 at 07:08
  • @DharmbirSingh these are the values sir 28.566540 77.209841 error i get these values from api – sandeep tomar May 25 '16 at 07:10
  • ok to be to the point tell me : NSLog(@"loc %@", LocationAtual); what it prints ? NSLog(@"placemark %@",placemark); what this prints ? NSLog(@"addressDictionary %@", placemark.addressDictionary); what this prints ? – maddy May 25 '16 at 07:35
  • sir i am checking step by step using breakpoints – sandeep tomar May 25 '16 at 07:36
  • placemark CLCircularRegion (identifier:'<+28.56713205,+77.21233975> radius 471.64', center:<+28.56713205,+77.21233975>, radius:471.64m) 2 placemark India placemark New Delhi location All India Institute Of Medical Sciences - AIIMS location (null) location 110029 location Ansari Nagar location <+28.56713200,+77.21182200> +/- 100.00m (speed -1.00 mps / course -1.00) @ 25/05/2016 13:11:55 India Standard Time 2016-05-25 13:11:55.759 GGR Driver Staging[13889:8711845] I am currently at All India Institute Of Medical Sciences - AIIMS, Ansari Nagar, New Delhi, Delhi 110029, India – sandeep tomar May 25 '16 at 07:44
  • City = "New Delhi"; Country = India; CountryCode = IN; FormattedAddressLines = ( "All India Institute Of Medical Sciences - AIIMS", "Ansari Nagar", "New Delhi", "Delhi 110029", India ); Name = "All India Institute Of Medical Sciences - AIIMS"; State = Delhi; SubAdministrativeArea = Delhi; SubLocality = "Ansari Nagar"; ZIP = 110029; – sandeep tomar May 25 '16 at 07:45
  • @Alok But sir when i tried to add breakpoints in block its not working – sandeep tomar May 25 '16 at 07:47
  • that's what @trojanfoe is telling you . This process is call Asynchronous (don't ever say control goes :) ). Your code is working perfectly. Now you can use Notification Center to be notified that your callback method have got some values you are interested in. – maddy May 25 '16 at 07:55
  • ok sir thanks for clear my confusion – sandeep tomar May 25 '16 at 08:38

2 Answers2

1

In viewDidLoad OfYour class or any other class just add:

- (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);
}

i have just add a single line in your code:

    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];
    }
    ];

Hope it will give you a overall learning

maddy
  • 4,001
  • 8
  • 42
  • 65
0

Try this code from Ram G answer of This Question it works for you

- (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);

    }];

}
Community
  • 1
  • 1