4

我一直在网上寻找这个解决方案。它与 iphone 上的谷歌地图应用程序完全一样,当您搜索时,它会通过地址簿查找有关您要查找的内容的建议。

到目前为止,我设法做的是设置一个搜索栏和一个地图视图,所以当我搜索确切的地址时,我会在地图上显示它。

我想让可能将地址存储在地址簿中的用户更容易使用它在地图视图上进行搜索。

我知道您需要使用搜索显示控制器和地址簿,但除了默认表格视图之外,我找不到任何搜索显示控制器的示例。

这就是我到目前为止所拥有的

编辑。

我想没有人知道。但是我花了几天时间才弄清楚。

这是每个想要学习如何做到这一点的人的解决方案。

这里要知道的关键是如何将地址簿数据放入一个数组中,您可以使用搜索显示控制器将其过滤掉。

所以这是我想出的获取通讯录数据的方法:

    - (void) getContactData
{
    NSMutableDictionary *myAddressBook = [[NSMutableDictionary alloc] init];

    self.names = [NSMutableArray array];





    ABAddressBookRef addressBook = ABAddressBookCreate();

    if (addressBook != nil){

        NSLog(@"Successfully accessed the address book.");

        NSArray *allPeople = (__bridge_transfer NSArray *) ABAddressBookCopyArrayOfAllPeople(addressBook);

        NSString *address;

        NSUInteger peopleCounter = 0;
        for (peopleCounter = 0;
             peopleCounter < [allPeople count]; peopleCounter++){

            ABRecordRef thisPerson = (__bridge ABRecordRef)

            [allPeople objectAtIndex:peopleCounter];

            NSString *firstName = (__bridge_transfer NSString *) ABRecordCopyValue(thisPerson, kABPersonFirstNameProperty);

            NSString *lastName = (__bridge_transfer NSString *) ABRecordCopyValue(thisPerson, kABPersonLastNameProperty);

            NSString *company = (__bridge_transfer NSString *) ABRecordCopyValue(thisPerson, kABPersonOrganizationProperty);


            ABMutableMultiValueRef multiValue = ABRecordCopyValue(thisPerson, kABPersonAddressProperty);
            for(CFIndex i=0;i<ABMultiValueGetCount(multiValue);i++)
            {
                NSString* HomeLabel = (__bridge NSString*)ABMultiValueCopyLabelAtIndex(multiValue, i);


                if([HomeLabel isEqualToString:@"_$!<Home>!$_"])
                {

                    CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(multiValue, i);
                    CFStringRef street = CFDictionaryGetValue(dict, kABPersonAddressStreetKey);
                    CFStringRef city = CFDictionaryGetValue(dict, kABPersonAddressCityKey);
                    CFStringRef state = CFDictionaryGetValue(dict, kABPersonAddressStateKey);
                    CFStringRef zip = CFDictionaryGetValue(dict, kABPersonAddressZIPKey);

                    CFRelease(dict);

                    if (zip == nil) {
                        address = [NSString stringWithFormat:@"%@, %@, %@ ", street, city, state];

                    }else {
                        address = [NSString stringWithFormat:@"%@, %@, %@ , %@", street, city, state, zip];
                    }


                } 



                if ([address length] != 0) {
                   // [self.addresses insertObject:address atIndex:i];
                    [myAddressBook setObject:address forKey:@"Address"];

                    if ([firstName length] != 0 && [lastName length] != 0) {
                        NSString *name= [NSString stringWithFormat: @"%@ %@", firstName, lastName];
                        [myAddressBook setObject:name forKey:@"Name"];
                        // [names insertObject:name atIndex:i];
                    }
                    else if ([firstName length] != 0 && [lastName length] == 0) {
                        NSString *name= [NSString stringWithFormat: @"%@", firstName];
                        // [names insertObject:name atIndex:i];
                        [myAddressBook setObject:name forKey:@"Name"];

                    }
                    else if ([firstName length] == 0 && [lastName length] == 0) {
                        if ([company length] != 0) {
                            //[names insertObject:company atIndex:i];
                            [myAddressBook setObject:company forKey:@"Name"];


                        }
                    }


                    NSString *name = [myAddressBook objectForKey:@"Name"];
                    NSString *address = [myAddressBook objectForKey:@"Address"];

                    NSDictionary* personDict = [[NSDictionary alloc] initWithObjectsAndKeys:name,@"Name",address,@"Address",nil];

                    [self.names addObject:personDict];

                }


            }


        }
        CFRelease(addressBook);



    }


}


     // Setup SearchBar
    UISearchBar *search = [[UISearchBar alloc] initWithFrame:CGRectMake(20, 0, 278, 44)];
    search.delegate = self;
    self.searchBar = search;

    // Setup Map View
    MKMapView *mapView = [[MKMapView alloc] initWithFrame:CGRectMake(20, 44, 278, 150)];
    mapView.mapType = MKMapTypeStandard;
    mapView.autoresizingMask =  UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.myMapview = mapView;

    [self.view addSubview:self.myMapview];
    [self.view addSubview:self.searchBar];

    - (void)searchBarSearchButtonClicked:(UISearchBar *)aSearchBar {

    // When the search button is tapped, add the search term to recents and conduct the search.
    NSString *searchString = [searchBar text];
    self.eventLocationCoordinate = [self getLocationFromAddressString:searchString];

    [self zoomMapAndCenterAtLatitude:self.eventLocationCoordinate.latitude andLongitude:self.eventLocationCoordinate.longitude];

    [self setMapAnnotationAtCoordinate:self.eventLocationCoordinate withTitle:@"Your Here" andSubtitle:searchString];
}

玩得开心编码!

4

0 回答 0