Andrew Paul Simmons
Andrew Paul Simmons

Reputation: 4523

How do I get cityname, province, state, from PHAsset in iOS 8 Photos Framework?

Is it possible to access the location information such as city name and state from a PHAsset. The iOS Photos app shows very specific location information such as Belltown, Seattle, Washington, USA. However, all that I've been able to access from a PHAsset is the CALocation from the location property. Which I believe only provides me with geo-coords and therefore would still require reverse-geocoding. I have thousands of photos to reverse geocode per user. So, I would like to avoid having to do reverse geocoding and simply access the human readable location information that Apple is already displaying in the Photos app.

In short, given a PHAsset object, how do I access the cityname for the photo location, without using a reverse geocoding service?

Upvotes: 4

Views: 1060

Answers (2)

Mykhailo Lysenko
Mykhailo Lysenko

Reputation: 164

In PHAsset there is a property for location, use it.

static NSString *const kCityString  = @"City";
@property (nonatomic, copy) NSString *cityString;

...

CLGeocoder *geocoder = [CLGeocoder new];
__weak typeof(self) weakSelf = self;

[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
    __strong typeof(weakSelf) strongSelf = weakSelf;

    CLPlacemark *placemark = placemarks.lastObject;

    // deprecated since iOS 11.0
    strongSelf.cityString = placemark.addressDictionary[kCityString];

    // or equivalent
    strongSelf.cityString = placemark.locality;
}];

go and look at CLPlacemarkfor other properties.

Upvotes: 1

Benjamin Berger
Benjamin Berger

Reputation: 142

You can use Google Maps API to perform geocoding translations on a latitude and longitude. See more here:

https://developers.google.com/maps/documentation/geocoding/#ReverseGeocoding.

Then you could cache the results and reuse them if you query locations that are very close. Another thing would be to use the GeoLite geo location database, but this is some 100 megs..

See here for download

Upvotes: 0

Related Questions