【问题标题】:MKLocalSearch not working like Apple Maps search?MKLocalSearch 不像 Apple 地图搜索那样工作?
【发布时间】:2015-11-16 17:01:03
【问题描述】:

我创建了一个 MapKit 并尝试使用 MKLocalSearch。与 Apple Maps 相比,我注意到的一件事是 mklocalsearch 限制为 10 个结果。那么 Apple Maps 是如何在搜索栏下显示 15 条建议的呢?

好的,举个例子。我试图找到“巴塞罗那”。在 Apple Maps 中,它会在只写“barc”后被建议,并且在输入 barcelona 的过程中它会一直保留在建议列表中。 现在在我自己的地图视图中,我实际上必须输入完整的巴塞罗那才能获得建议:西班牙,巴塞罗那。在途中,我得到了其他建议,但没有像西班牙、巴塞罗那和 Apple 地图那样。

关于如何使其工作以及为什么 Apple 地图工作方式不同的任何见解(说明 15 个结果与 mklocalseach 的 10 个结果)

这是在 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];
 }];

}

【问题讨论】:

  • 因此地图应用可以使用您无法使用的私有 API。你对此感到惊讶吗?这就像邮件应用程序中的滑动消息列表单元格一样——Apple 做了一些他们通常没有添加到 Cocoa Touch 中的事情。这很烦人,但并不少见。
  • 嗯,我有点惊讶,说我的例子不可能获得理智的结果。我不喜欢我必须使用谷歌 API,因为 ios6(我认为?)苹果说不再需要谷歌。
  • @DevilInDisguise,有什么建议吗?我有同样的问题

标签: ios mapkit mklocalsearch


【解决方案1】:

您所做的是使用 MKLocalSearchRequest 的 MKLocalSearch。 Apple 在其 macOS 和 iOS 地图应用程序中所做的是使用更新的 MKLocalSearchCompleter 类来获取自动补全建议。这些建议用于实时搜索并显示在 UITableView 中。当用户选择一个条目时,该建议用于初始化 MKLocalSearchRequest 以获取有关此位置的详细信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    • 1970-01-01
    • 1970-01-01
    • 2017-04-13
    • 1970-01-01
    • 2017-12-25
    相关资源
    最近更新 更多