2016-10-19 12 views
0

私はCLLocationの画像の緯度と経度をカメラロールで取得するアプリを持っています。私はそれらの座標のアドレスを得ることができます。そのアドレスを私の連絡先のアドレスと比較するためのアドレスがありますか?一致するものがある場合は、その連絡先アドレスに対応する名前のNSStringを作成します。CLLocationを使用してアドレス帳の連絡先を取得するXcode

//最初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); 
    } 

答えて

0

あなたかもしれ役に立つ連絡先帳からこのチュートリアルを住所を取得するには、住所を連絡するためにあなたのアドレスを比較だから私は

これを考え出しました私は、資産の場所

  CLLocation *location = [asset valueForProperty:ALAssetPropertyLocation]; 

//を取得//

if (picker.sourceType ==UIImagePickerControllerSourceTypePhotoLibrary) { 

    NSURL * movieURL = [info valueForKey:UIImagePickerControllerMediaURL] ; 

[self getMediaName:nil url:[info objectForKey:UIImagePickerControllerReferenceURL]]; 
    } 


- (void)getMediaName:(UIImage*)originalImage url:(NSURL*)url { 
    @try { 
     ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *asset) { 
      if (asset == nil) return; 
      ALAssetRepresentation *assetRep = [asset defaultRepresentation]; 

私は、構成部品

 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); 
          NSString *strString =placemark.name; 
           NSString *midstring = [strString stringByAppendingString:@","]; 
           if (placemark.postalCode == nil) { 
            postalcodeString = @"00000"; 
           } 
           else{ 
            postalcodeString = placemark.postalCode; 
           } 

           combineString = [midstring stringByAppendingString:postalcodeString]; 
           NSLog(@"combineString:%@",combineString); 

//その後、ユーザーのアドレス帳にアクセスして表示しますそれぞれの人の住所と郵便番号を入力し、それらをまとめて文字列にします。

       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); 

            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) { 
             CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(address, 0); 
             NSString *streetString = CFDictionaryGetValue(dict, kABPersonAddressStreetKey); 
             NSString *zipString = CFDictionaryGetValue(dict, kABPersonAddressZIPKey); 
             if (zipString == Nil) { 
              zipString = @"00000"; 
             } 
             NSString *interstring = [streetString stringByAppendingString:@","]; 
             NSString *allString = [interstring stringByAppendingString:zipString]; 
             NSLog(@"allstring;%@ %@",allString, combineString); 

//次に、位置文字列とアドレス文字列を比較します。一致がある場合は、そのアドレスの人物の名前を保存し、後でアドレスに添付してテキストビューを記入します。

         if ([combineString isEqualToString:allString]) { 
              NSLog(@"its a match"); 
              NSLog(@"i:%d",i); 
              firstNameString = firstName; 

             } 

             else { 

             } 


             NSLog(@"streetString:%@",streetString); 
             NSLog(@"firstNameString:%@",firstNameString); 
             NSLog(@"combineString:%@",combineString); 
                     } 
           }} 
          else { 
           NSLog(@"Could not locate"); 
          } 
         } 
      ]; 
      // Do what you need with the file name here 
     }; 

     ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *error) { 

     }; 

     ALAssetsLibrary *library = [ALAssetsLibrary new]; 
     [library assetForURL:url resultBlock:resultblock failureBlock:failureblock]; 
    } 
    @catch (NSException *exception) { 

    } 

ありがとうございました。

関連する問題