I have an app in which I get the CLLocation latitude and longitude of an image in the camera roll. I can get the address of those coordinates. Is there someway to compare that address to the addresses of my contacts and if there is a match create an NSString with the name corresponding with that contact address.
//first get the CLLocation
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *asset) {
if (asset == nil) return;
ALAssetRepresentation *assetRep = [asset defaultRepresentation];
CLLocation *location = [asset valueForProperty:ALAssetPropertyLocation];
NSLog(@"lat;%f",location.coordinate.latitude);
NSLog(@"long;%f",location.coordinate.longitude);
//then find the address
CLGeocoder *ceo = [[CLGeocoder alloc]init];
CLLocation *loc = [[CLLocation alloc]initWithLatitude:location.coordinate.latitude longitude:location.coordinate.longitude]; //insert your coordinates
[ceo reverseGeocodeLocation:loc
completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
if (placemark) {
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);
//Print the location to console
NSLog(@"I am currently at %@",locatedAt);
}
}];
so is there a way to compare this address with the ones in the users contacts to see if there is a match
and then create an NSString with the name of that contact
So I have been able to get access to the address book and get the first and last name and the index they reside at.
I have also been able to find out the address entered for each contact, however it is in a form I can't use as I need to make it so I can compare the location of the picture to the location in the address book.
below is my code. Can anyone help me figure out how to get the address of each contact so I can either compare the street address and zip code or convert the address to a set of coordinate that could be compared
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
if (granted) {
// First time access has been granted, add the contact
NSLog(@"granted");
} else {
// User denied access
// Display an alert telling user the contact could not be added
NSLog(@"denied");
}
});
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
// The user has previously given access, add the contact
NSLog(@"authorized");
}
else {
// The user has previously denied access
// Send an alert telling user to change privacy setting in settings app
}
CFErrorRef *error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
//ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);
for(int i = 0; i < numberOfPeople; i++) {
NSLog(@"i;%d",i);
NSMutableDictionary *dOfPerson=[NSMutableDictionary dictionary];
ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );
NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
NSLog(@"Name:%@ %@", firstName, lastName);
ABMutableMultiValueRef address = (ABRecordCopyValue(person, kABPersonAddressProperty));
if(ABMultiValueGetCount(address) > 0) {
[dOfPerson setObject:(__bridge NSString *)ABMultiValueCopyValueAtIndex(address, 0) forKey:@"City"];
NSString *addressste = [dOfPerson valueForKey:@"City"];
NSLog(@"dOfPerson;%@",[dOfPerson description]);
NSLog(@"streetadr:%@",addressste);
}
NSLog(@"address:%@",address);
}
So I figured this out
//I get the asset location
//I break that into the component parts
//then I reverse location to get a street address and Zip Code and make a string from those
//Then I access the user's address book and look for the street address and zip codes of each person and put those together into a string.
//Then I compare the location string to the address string. If there is a match I save the name of the person of that address and attach it to the address later to fill in a textview.
Thanks for everyone's help.