Use MKLocalSearch to find locations on a map.

I want to use MKLocalSearch to search on a map. This functionality is available in iOS 6.1+. Does anyone know how to use this, or can someone give an example of how to use MKLocalSearch ?

MKLocalSearchResponse Documentation

+6
source share
3 answers

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); // Should use a weak copy of self [self.mapView addAnnotation:[mapItem placemark]]; } } else { NSLog(@"Search Request Error: %@", [error localizedDescription]); } }]; 

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); } 
+21
source

Here is an example of a cafe search within a radius of 1 km around a certain place:

 MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init]; CLLocationCoordinate2D location = CLLocationCoordinate2DMake(11.567898, 104.894430); request.naturalLanguageQuery = @"cafe"; request.region = MKCoordinateRegionMakeWithDistance(location, 1000, 1000); MKLocalSearch *search = [[MKLocalSearch alloc] initWithRequest:request]; [search startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error){ for (MKMapItem *item in response.mapItems) { NSLog(@"%@", item.name); } }]; 

Please note that if the search is unsuccessful, it does not return an empty list, but an error with the MKErrorDomain domain and code 4 .

+6
source
+1
source

All Articles