I have a search bar on my map view that allows the user to search for the name (annotation title) or address (annotation subtitle). I have it working just fine if the user types in the entire title or subtitle but I'm trying to have it so the user only has to type part of the name or address. For example, there is an annotation for Cates Farm. I'd like the user to be able to just type in cates and have it find the annotation. The way I have it set up not only just finds the last title in the array, but it runs slowly. Any help or suggestions?
Here is my code. The lines I've commented out are from where I had it working if the user types the entire title or subtitle.
Here is my code. The lines I've commented out are from where I had it working if the user types the entire title or subtitle.
Code:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
id<MKAnnotation> ann;
NSString *searchText = [searchBar text];
NSString *annTitle = ann.title;
NSString *annSubtitle = ann.subtitle;
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];
}