The API for MKLocalSearch pretty easy to understand. Mostly, you
alloc-init a MKLocalSearchRequest- Set
naturalLanguageQuery to any search query - Use the search query to initialize the
MKLocalSearch object - Tell start a local search by passing it a completion handler.
- Do something with an array of
MKMapItem objects in the answer
Cafe search:
// Create a search request with a string MKLocalSearchRequest *searchRequest = [[MKLocalSearchRequest alloc] init]; [searchRequest setNaturalLanguageQuery:@"Cafe"]; // Create the local search to perform the search MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:searchRequest]; [localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) { if (!error) { for (MKMapItem *mapItem in [response mapItems]) { NSLog(@"Name: %@, Placemark title: %@", [mapItem name], [[mapItem placemark] title]); } } else { NSLog(@"Search Request Error: %@", [error localizedDescription]); } }];
You can specify the region to search, for example:
// Search for Cafes in Paris MKLocalSearchRequest *searchRequest = [[MKLocalSearchRequest alloc] init]; [searchRequest setNaturalLanguageQuery:@"Cafe"]; CLLocationCoordinate2D parisCenter = CLLocationCoordinate2DMake(48.8566667, 2.3509871); MKCoordinateRegion parisRegion = MKCoordinateRegionMakeWithDistance(parisCenter, 15000, 15000); [searchRequest setRegion:parisRegion];
You can also take an area from MKMapView that the user has enlarged. This will give the best results:
[searchRequest setRegion:self.mapView.region]
The MKLocalSearchResponse response MKLocalSearchResponse contains an array of MKMapItem objects ( mapItems ) and a MKCoordinateRegion , called boundingRegion , which is the area containing all the results. You can use it to display the map view to show all the results:
[self.mapView setRegion:response.boundingRegion]
An array of MKMapItem objects cannot be placed on the map (they are used to send to the Maps application), but each of them contains a placemark property that can be added to the map:
[localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) { if (!error) { for (MKMapItem *mapItem in [response mapItems]) { NSLog(@"Name: %@, MKAnnotation title: %@", [mapItem name], [[mapItem placemark] title]); NSLog(@"Coordinate: %f %f", [[mapItem placemark] coordinate].latitude, [[mapItem placemark] coordinate].longitude);
Dublin Search puts a pin on the map view and logs:
Name: Dublin, Co. Dublin, MKAnnotation title: Dublin, Co. Dublin, Ireland Coordinate: 53.344104 -6.267494
The returned objects are loaded with additional information, especially if you are looking for a business. Here are a few:
[localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) { if (!error) { NSLog(@"Results: %@", [response mapItems]); MKMapItem *mapItem = [[response mapItems] objectAtIndex:0]; NSLog(@"Name:%@ Phone:%@ URL:%@", [mapItem name], [mapItem phoneNumber], [mapItem url]); NSLog(@"Placemark: %@", [mapItem placemark]); MKPlacemark *placemark = [mapItem placemark]; NSLog(@"Placemark Address: %@", [placemark addressDictionary]); MKCoordinateRegion boundingRegion = [response boundingRegion]; NSLog(@"Bounds: %f %f", boundingRegion.span.latitudeDelta, boundingRegion.span.longitudeDelta); }