Searching Annotations with UISearchBar


#1

I have my map view with all of my annotations in an array. On my map view, I have a UISearchBar that I’m using to be able to search for annotations by name (annotation title) or by address (annotation subtitle). Once it find the annotation it selects that annotation. I’m able to get it working if I search for the full title or subtitle but I want to be able to search by typing in a partial title or subtitle. For example, there is an annotation with a title of “Cates Farm”. I want to be able to type “cates” and have it find that annotation. The way I have it set up, it always brings up the title of the last annotation in the array, even if I search for something I know doesn’t exist. The search also runs very slow. Can anyone help?

[code]- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
id ann;
NSString *searchText = [searchBar text];
NSString *annTitle = ann.title;
NSString *annSubtitle = ann.subtitle;
// NSString *marketTitle = marketAnnotation.title;
// NSString *marketSubtitle = marketAnnotation.subtitle;
// NSRange titleRange = [marketTitle rangeOfString:searchText options:NSCaseInsensitiveSearch];
// NSRange subtitleRange = [marketSubtitle rangeOfString:searchText options:NSCaseInsensitiveSearch];
NSRange titleRange = [annTitle rangeOfString:searchText options:NSCaseInsensitiveSearch];
NSRange subtitleRange = [annSubtitle rangeOfString:searchText options:NSCaseInsensitiveSearch];

for (int i = 0; i < [marketLocations count]; i++)
{
    for (ann in marketLocations)
    {
        // if ([ann.title isEqualToString:[searchBar text]])
        if (titleRange.location != NSNotFound)
        {
            [worldView selectAnnotation:ann animated:YES];
        }
        // else if ([ann.subtitle isEqualToString:[searchBar text]])
        else if (subtitleRange.location != NSNotFound)
        {
            [worldView selectAnnotation:ann animated:YES];
        }
    }
}

[searchBar resignFirstResponder];

}
[/code]