Add SearchDisplayController Over mapKit to search Address book

1.7k views Asked by At

I've been looking the web for this solution. Its exactly like the google maps application on the iphone, where as you search, it looks through the address book to find suggestions for what your looking for.

What i managed to do so far is set up a search bar and a mapview, so when i search for the exact address i get it displayed on the map.

I want to make it easier for the users, who might have addresses stored in their address book to use it to search on the map view.

I know that you need to use search display controller and address book, but i can't find any examples of search display controller on other than a default table view.

this is what i have so far

EDIT.

I guess no one knows. But it took me a couple of days to figure it out.

Heres the solution for everyone wanting to learn how to do this.

The key thing to know here is how to get the address book data into an array that you can just use with the search display controller to filter it out.

So here is the method i came up with to get the address book data:

    - (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];
}

Have fun coding!

0

There are 0 answers