有没有办法在GMSAutocompleteResultsViewController中显示自定义预定义结果,如家庭/工作地址,收藏夹或最近的地方?
我在UserDefaults中保存的地点在引入任何文本输入之前,在点击搜索栏时不知道如何显示它们。
我设法使用MapKit和自定义表格视图执行此操作:
func updateSearchResults(for searchController: UISearchController) {
print("----UPDATE SEARCH RESULTS")
guard let mapView = mapView,
let searchBarText = searchController.searchBar.text
else {
return
}
matchingItems = []
if(searchBarText == " "){
print("Text: SPACE")
if let location = currentLocation{
matchingItems = [location]
}
if(home != nil){
addressToPlacemark(searchParam: home!, region: nil, reload: false)
}
if(work != nil){
addressToPlacemark(searchParam: work!, region: nil, reload: false)
}
} else if(searchBarText.characters.count >= 3){
addressToPlacemark(searchParam: searchBarText, region: mapView.region, reload: true)
} else{
self.tableView.reloadData()
}
}
func addressToPlacemark(searchParam: String, region: MKCoordinateRegion?, reload: Bool) {
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = searchParam
if let region = region{
request.region = region
}
let search = MKLocalSearch(request: request)
search.start { (response, error) in
print("ERROR: \(String(describing: error))")
guard let response = response else {
print("NO VALID RESPONSE")
self.tableView.reloadData()
return
}
self.matchingItems.append(contentsOf: response.mapItems.map({ (item:MKMapItem) -> MKPlacemark in
item.placemark
}))
var uniqueItems:[MKPlacemark] = []
for item in self.matchingItems{
let first = uniqueItems.first(where: { (uniqueItem:MKPlacemark) -> Bool in
uniqueItem.title == item.title && uniqueItem.coordinate.latitude == item.coordinate.latitude && uniqueItem.coordinate.longitude == item.coordinate.longitude
})
if(first == nil){
uniqueItems.append(item)
}
}
self.matchingItems = uniqueItems
if(reload || self.matchingItems.count == self.savedAddressesCount){
print("Reloaded data")
self.tableView.reloadData()
}
}
}
我已被欺骗" UISearchController通过以编程方式在单击时添加空格来显示表视图:
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
print("searchBarTextDidBeginEditing - CLICK")
searchBar.text = " "
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if(searchText.characters.count > 1 && searchText.hasPrefix(" ")){
searchBar.text = searchText.substring(from: searchText.index(searchText.startIndex, offsetBy: 1))
print("searchBarTextDidChange - TRIM")
}else if(searchText.isEmpty){
searchBar.text = " "
print("searchBarTextDidChange - EMPTY")
}
}
我可以使用Google地图做类似的事吗?这是否可以通过使用GMSAutocompleteResultsViewController或我应该使用自定义ResultsViewController?