DevilInDisguise
DevilInDisguise

Reputation: 360

MKLocalSearch not working like Apple Maps search?

I have created a MapKit and trying to play around with MKLocalSearch. One thing I noticed in comparison to Apple Maps, is that mklocalsearch is restricted to 10 results. So how does Apple Maps display 15 suggestions under the search bar?

Okay, on to an example. Im trying to find "Barcelona." In Apple Maps it will be suggested after writing just "barc" and it will stay on the suggestion list throughout typing barcelona. Now in my own Map view, I actually have to type in the full Barcelona to get the suggestion: Spain, Barcelona. On my way I get other suggestions, but nothing like Spain, Barcelona and not like Apple maps.

Any insight on how to get it working and to why Apple Maps work differently (spec. the 15 results vs 10 with mklocalseach)

Here is the code called on textField Changes:

- (IBAction)searchFieldChanged:(UITextField *)sender {
if(self.locationTextfield.text.length>0)
    self.tableView.hidden = NO;
else
    self.tableView.hidden = YES;

NSString *query = self.locationTextfield.text;
// Create and initialize a search request object.
MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
request.naturalLanguageQuery = query;
request.region = self.mapsView.region;//we dont want region-specific search results!
//request.region = MKCoordinateRegionMakeWithDistance(self.mapsView.userLocation.location.coordinate,40000000, 15000000);

// Create and initialize a search object.
MKLocalSearch *search = [[MKLocalSearch alloc] initWithRequest:request];

// Start the search and display the results as annotations on the map.
[search startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error)
 {
     [placeMarks removeAllObjects];
     NSLog(@"p-count: %lu", response.mapItems.count);
     for (MKMapItem *item in response.mapItems) {
         [placeMarks addObject:item.placemark];
         self.tempPlacemark = item.placemark;
         NSLog(@"placemark: %@", item.placemark);//.location.coordinate.latitude);

     }
     //if(placemarks.count==0)
     // appDelegate.staticPlacemark = nil;


     //[self.mapsView removeAnnotations:[self.mapsView annotations]];
     //[self.mapsView showAnnotations:placemarks animated:NO];
     [self.tableView reloadData];
 }];

}

Upvotes: 1

Views: 927

Answers (1)

blackjacx
blackjacx

Reputation: 10520

What you do is a MKLocalSearch using a MKLocalSearchRequest. What Apple in its macOS and iOS map apps does is using the newer MKLocalSearchCompleter class to obtain autocompletion suggestions. These suggestions are used for realtime search and displayed in a UITableView. When the user selects one entry that suggestion is used to initialize a MKLocalSearchRequest to obtain detailled information about this location.

Upvotes: 1

Related Questions