Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  2. return matchingItems.count
  3. }
  4.  
  5. func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath : IndexPath) -> UITableViewCell {
  6. let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! LocationCell
  7. let selectedItem = matchingItems[indexPath.row].placemark
  8.  
  9. cell.titleLabel.text = selectedItem.name
  10. cell.subtitleLabel.text = parseAddress(selectedItem: selectedItem)
  11. return cell
  12. }
  13.  
  14. func updateSearchResults(for searchController: UISearchController) {
  15. matchingItems.removeAll()
  16. guard let mapView = mapView, let searchBarText = searchController.searchBar.text else { return }
  17. let request = MKLocalSearch.Request()
  18. request.naturalLanguageQuery = searchBarText
  19. request.region = mapView.region
  20.  
  21. let search = MKLocalSearch(request: request)
  22.  
  23. search.start(completionHandler: {(response, error) in
  24. if let results = response {
  25. if let err = error {
  26. print("Error occurred in search: (err.localizedDescription)")
  27. } else if results.mapItems.count == 0 {
  28. print("No matches found")
  29. } else {
  30. print("Matches found")
  31.  
  32. for item in results.mapItems {
  33. print("Name = (item.name ?? "No match")")
  34. print("Phone = (item.phoneNumber ?? "No Match")")
  35.  
  36. self.matchingItems.append(item as MKMapItem)
  37. print("Matching items = (self.matchingItems.count)")
  38.  
  39. let annotation = MKPointAnnotation()
  40. annotation.coordinate = item.placemark.coordinate
  41. annotation.title = item.name
  42. self.mapView!.addAnnotation(annotation)
  43. }
  44. self.tableView.reloadData()
  45. }
  46. }
  47. })
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement