1

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];
 }];

}
DevilInDisguise
  • 360
  • 1
  • 4
  • 14
  • So the Maps app can use private API that you can't use. Are you surprised by that? It's just like the sliding message list cells in the Mail app - Apple does something they have not added to Cocoa Touch generally. It's annoying but it's not uncommon. – matt Nov 16 '15 at 17:20
  • Well, Im a bit surprised that it wouldn't be possible to obtain sane results from say my example. I dont like that I have to go with Google API, also since ios6 (I think?) apple said no more need for google. – DevilInDisguise Nov 16 '15 at 22:22
  • 1
    @DevilInDisguise, any suggestions ? I have a same issue – dev.nikolaz Oct 13 '16 at 16:14

1 Answers1

1

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.

blackjacx
  • 9,011
  • 7
  • 45
  • 56