简体   繁体   English

将项目添加到对象

[英]Adding items into Objects

I'm looking to be able to append a personal location into the MKLocationSearchCompletion array that will can be found when the user searches through the search bar. 我希望能够将个人位置附加到MKLocationSearchCompletion array ,该array将在用户通过搜索栏进行搜索时找到。 However, I am having trouble understanding how items are stored into objects and whether I can add a placemark object (or location information) into the MKLocationSearch object. 但是,我在理解如何将项目存储到对象以及是否可以在MKLocationSearch对象中添加地标对象(或位置信息)时遇到麻烦。 What I've been able to garner from the documentation is that the MKLocalSearchCompleter object stores strings that are accessed when the user types in partial strings into the search bar. 我从文档中可以得到的是, MKLocalSearchCompleter对象存储当用户在搜索栏中键入部分字符串时可以访问的字符串。 But I am not sure where I can access this array and add new locations. 但是我不确定在哪里可以访问此数组并添加新位置。

Here is how the code is structured to display search completion results: 这是代码的结构,以显示搜索完成结果:

var searchCompleter = MKLocalSearchCompleter()
var searchResults = [MKLocalSearchCompletion]()

@IBOutlet weak var searchBar: UISearchBar!

override func viewDidLoad() {
    searchCompleter.delegate = self
    searchBar.delegate = self
}

extension ViewController: MKLocalSearchCompleterDelegate {
    func completerDidUpdateResults(_ completer: MKLocalSearchCompleter) {
        searchResults = completer.results
        searchResultsTableView.reloadData()
    }

    func completer(_ completer: MKLocalSearchCompleter, didFailWithError error: Error) {
        // handle error
    }
}

extension ViewController: UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return searchResults.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let searchResult = searchResults[indexPath.row]
        let cell = UITableViewCell(style: .subtitle, reuseIdentifier: nil)

        cell.textLabel?.attributedText = highlightedText(searchResult.title, inRanges: searchResult.titleHighlightRanges, size: 17.0)
        cell.detailTextLabel?.attributedText = highlightedText(searchResult.subtitle, inRanges: searchResult.subtitleHighlightRanges, size: 12.0)

        return cell
    }
}

extension ViewController: UISearchBarDelegate {
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        searchCompleter.queryFragment = searchText
    }

    func searchBarShouldEndEditing(_ searchBar: UISearchBar) -> Bool {
        self.searchBar.endEditing(true)
        searchBar.resignFirstResponder()
        return true
    }
}

I don't think that you can add your own locations and POIs to MapKit, but: 我认为您不能将自己的位置和POI添加到MapKit,但是:

1) I would suggest you create an own enum 1)我建议您创建一个自己的枚举

class CustomSearchResult {
    let title: String
    ...
}

enum SearchResultType {
    case localSearchResult(result: MKLocalSearchCompletion)
    case customResult(result: CustomSearchResult)
}

2) And you have your array of results: 2)您将得到一系列结果:

var searchResults = [SearchResultType]()

3) In completerDidUpdateResults you can add your personal results and the MapKit results into your searchResults array: 3)在completerDidUpdateResults您可以将个人结果和MapKit结果添加到searchResults数组中:

searchResults = completer.results.map { 
   SearchResultType.localSearchResult(result: $0) }

// Add here custom results
searchResults.append(SearchResultType.customResult(result: 
    CustomSearchResult(title: "test")))

4) ..and in cellForRowAtIndexPath you can decide whether you have custom or MapKit result: 4)..并在cellForRowAtIndexPath您可以确定是否具有自定义或MapKit结果:

let searchResult = searchResults[indexPath.row]
switch searchResult {
case .customResult(let result):
    cell.textLabel.text = result.title
case .localSearchResult(let result):
    cell.textLabel.text = result.title
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM