I’m able to retrieve the current location in my iPad application using,
CLLocation *location = [[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error)
{
NSLog(@"-----------------Placemark is %@-----------------", placemarks);
locationLabel.text = placemarks;
}];
and the output is,
-----------------Placemark is ("South Atlantic Ocean, South Atlantic Ocean @<-42.60533670,-21.93128480> +/- 100.00m, region (identifier <-41.51023865,-31.60774370> radius 4954476.31) <-41.51023865,-31.60774370> radius 4954476.31m"
)-----------------
Can I use the same information to just get the city and the country name? instead of the long list of information?
also, the ‘locationLabel.text = placemarks’ gives a warning, “Incompatible pointer types assigning to ‘NSString*’ from ‘NSArray*_strong’, which I’m unable to resolve.
Yes you can.
But you doing it a little it wrong. First of all,
placemarksis an array and not a string. That’s whylocationLabel.text = placemarksgives a warning.Placemarksis an array of CLPlacemarks. This is because the geocoder could return multiple results for a coordinate. In the simplest condition the first item in it should be okay.A
CLPlacemarkhas the propertyaddressDictionarywhich contains the data of this location.You can access this data with the address property constans defined by the ABPerson header file.
For example:
Get the first placemark from the array:
then get the city from this placemark:
Don’t forget to import the AVPerson header!