sandeep tomar sandeep tomar - 6 months ago 28
iOS Question

How to get place name from lattitude and longitude?

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.

Answer

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