【发布时间】:2017-02-28 14:12:32
【问题描述】:
我有一个应用程序,可以在其中获取相机胶卷中图像的CLLocation 纬度和经度。我可以得到那些坐标的地址。有没有办法将该地址与我的联系人地址进行比较,如果匹配,则创建一个NSString,其名称与该联系人地址对应。
//首先获取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);
//然后找到地址
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);
}
}];
所以有没有办法将此地址与用户联系人中的地址进行比较,以查看是否匹配
然后使用该联系人的姓名创建一个NSString
所以我已经能够访问地址簿并获得名字和姓氏以及他们所在的索引。
我还能够找到为每个联系人输入的地址,但它是我无法使用的表格,因为我需要制作它,以便我可以将图片的位置与地址中的位置进行比较书。
下面是我的代码。谁能帮我弄清楚如何获取每个联系人的地址,以便我可以 比较街道地址和邮政编码或 将地址转换为一组可以比较的坐标
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);
}
【问题讨论】:
标签: ios xcode contacts cllocation alasset