【问题标题】:Searching for annotations in a map在地图中搜索注释
【发布时间】:2025-12-04 00:40:01
【问题描述】:

我想知道是否有人知道是否可以在地图视图中提供搜索功能? 我希望能够使用 UISearchbar 搜索/过滤注释,就像在 tableview UI 中所做的那样。注释是从 plist 加载的。 很乐意提供任何提示或帮助,一直在搜索,但我找不到任何东西......

【问题讨论】:

  • 那么你可以做的是搜索从 plist 加载的项目数组。然后只需在MKMapView 上绘制找到的项目,放大以仅显示地图上的注释。

标签: ios search filter annotations mkmapview


【解决方案1】:

使用谓词按名称过滤它们。例如:

NSArray *filteredAnnotations = [self.mapView.annotations filterUsingPredicate:[NSPredicate predicateWithFormat:@"SELF.name beginswith[dc] %@",userInput]];

然后删除所有,并使用过滤数组中的注释重新加载地图。

[self.mapView removeAnnotations:self.mapView];
self.mapView.annotations = filteredAnnotations;

UISearchBar 的委托中的方法searchBarTextDidEndEditing: 触发此代码。如果您不想要搜索的表格预览,请使用不带 UISearchBarDisplayController 的 UISearchBar。

或者正如 rckoenes 所说,放大过滤后的注释区域。 SO中可能有相应的代码。

【讨论】: