Add SearchDisplayController Over mapKit to Search Address Book

I searched the internet for this solution. This is exactly the same as the google maps app on the iphone, where you search through your address book to find offers for your search.

What I have managed to do so far is to create a search bar and mapview, so when I look for the exact address, I get it on the map.

I want to make it easier for users who may have addresses stored in their address book to use them to search on a map.

I know that you need to use the search display controller and the address book, but I cannot find any examples of the search display controller other than the default table view.

that's what i still

EDIT.

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

This solution is for everyone who wants to learn how to do it.

The key here is to get the address book data into an array, which you can use with the search display controller to filter it.

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!

+4
source share

Source: https://habr.com/ru/post/1380326/


All Articles