Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return matchingItems.count
- }
- func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath : IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! LocationCell
- let selectedItem = matchingItems[indexPath.row].placemark
- cell.titleLabel.text = selectedItem.name
- cell.subtitleLabel.text = parseAddress(selectedItem: selectedItem)
- return cell
- }
- func updateSearchResults(for searchController: UISearchController) {
- matchingItems.removeAll()
- guard let mapView = mapView, let searchBarText = searchController.searchBar.text else { return }
- let request = MKLocalSearch.Request()
- request.naturalLanguageQuery = searchBarText
- request.region = mapView.region
- let search = MKLocalSearch(request: request)
- search.start(completionHandler: {(response, error) in
- if let results = response {
- if let err = error {
- print("Error occurred in search: (err.localizedDescription)")
- } else if results.mapItems.count == 0 {
- print("No matches found")
- } else {
- print("Matches found")
- for item in results.mapItems {
- print("Name = (item.name ?? "No match")")
- print("Phone = (item.phoneNumber ?? "No Match")")
- self.matchingItems.append(item as MKMapItem)
- print("Matching items = (self.matchingItems.count)")
- let annotation = MKPointAnnotation()
- annotation.coordinate = item.placemark.coordinate
- annotation.title = item.name
- self.mapView!.addAnnotation(annotation)
- }
- self.tableView.reloadData()
- }
- }
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement