Local search implementation using MKLocalSearch
suggest changeMKLocalSearch lets users search for a location with a natural-language string such as “gym”. When the search finishes, it returns locations inside a region that match the string.
Search results are MKMapItem values on an MKLocalSearchResponse. Import MapKit, and _mapView must be an MKMapView. A failed search passes a nil response, which would otherwise look like no matches.
Let's try an example:
MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
request.naturalLanguageQuery = @"Gym";
request.region = _mapView.region;
MKLocalSearch *search = [[MKLocalSearch alloc] initWithRequest:request];
[search startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
if (error != nil) {
NSLog(@"Search failed: %@", error);
return;
}
if (response.mapItems.count == 0) {
NSLog(@"No Matches");
} else {
for (MKMapItem *item in response.mapItems) {
NSLog(@"name = %@", item.name);
NSLog(@"Phone = %@", item.phoneNumber);
}
}
}];
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents