I want to allow users to search for the street name and display the results in a UITableView. At the moment, the region is not important, it can be from any region.
I could not find a suitable example in my searches, and I do not know whether to use CLLocation or MKLocalSearch.
Based on the docs, I should use MKLocalSearch:
Although local search and geocoding are similar, they support various use cases. Use geocoding if you want to convert between map coordinates and a structured address, such as an address book address. Use local search if you want to find a set of places that match user input.
But I tried both methods, and it gives me only 1 result (even if there is an NSArray.
This is the CLGeocoder approach:
CLGeocoder *geocoding = [[CLGeocoder alloc] init]; [geocoding geocodeAddressString:theTextField.text completionHandler:^(NSArray *placemarks, NSError *error) { if (error) { NSLog(@"%@", error); } else { NSLog(@"%i", [placemarks count]); for(CLPlacemark *myStr in placemarks) { NSLog(@"%@", myStr); } } }];
And this is my attempt at MKLocalSearch:
MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init]; request.naturalLanguageQuery = theTextField.text; request.region = self.region; localSearch = [[MKLocalSearch alloc] initWithRequest:request]; [localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error){ if (error != nil) { [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Map Error",nil) message:[error localizedDescription] delegate:nil cancelButtonTitle:NSLocalizedString(@"OK",nil) otherButtonTitles:nil] show]; return; } if ([response.mapItems count] == 0) { [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"No Results",nil) message:nil delegate:nil cancelButtonTitle:NSLocalizedString(@"OK",nil) otherButtonTitles:nil] show]; return; } self.streets = response; [self.streetsTableView reloadData]; }];
In some cases, MKLocalSearch returns more than 1 answer, but they are associated with searches for places where street names are not used.
Thanks in advance.
objective-c xcode cllocation mklocalsearch
Cristic
source share