So I have this navigation app in which a user can search for a location in a search table. If they type in a store like Mcdonald's, Target etc it will always appear correctly. However if you type a specific address it won't show up. Here's my tableView code for the locationSearchTable:
import UIKit
import MapKit
class LocationSearchTable : UITableViewController {
var handleMapSearchDelegate:handleMapSearch? = nil
var matchingItems:[MKMapItem] = []
var mapView: MKMapView? = nil
var resultSearchController:UISearchController? = nil
func parseAddress(selectedItem: MKPlacemark) -> String {
// put a space between "4" and "Melrose Place"
let firstSpace = (selectedItem.subThoroughfare != nil && selectedItem.thoroughfare != nil) ? " " : ""
// put a comma between street and city/state
let comma = (selectedItem.subThoroughfare != nil || selectedItem.thoroughfare != nil) && (selectedItem.subAdministrativeArea != nil || selectedItem.administrativeArea != nil) ? ", " : ""
// put a space between "Washington" and "DC"
let secondSpace = (selectedItem.subAdministrativeArea != nil && selectedItem.administrativeArea != nil) ? " " : ""
let addressLine = String(
format:"%@%@%@%@%@%@%@",
// street number
selectedItem.subThoroughfare ?? "",
firstSpace,
// street name
selectedItem.thoroughfare ?? "",
comma,
// city
selectedItem.locality ?? "",
secondSpace,
// state
selectedItem.administrativeArea ?? ""
)
return addressLine
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return matchingItems.count - 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "searchCell") as! ResultsDisplayCell
let selectedIndex = self.matchingItems[indexPath.row]
cell.titleLabel?.text = selectedIndex.name
cell.detailTextLabel?.text = selectedIndex.placemark.title
//cell.detailTextLabel?.text =
//cell.detailTextLabel?.text = selectedIndex.name
//cell.detailTextLabel?.text = selectedIndex.placemark.subtitle
return cell
}
}
extension LocationSearchTable : UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
guard let mapView = mapView,
let searchBarText = searchController.searchBar.text else { return }
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = searchBarText
request.region = mapView.region
let search = MKLocalSearch(request: request)
search.start { response, _ in
guard let response = response else {
return
}
for mapItems in response.mapItems{
self.matchingItems.append(mapItems as MKMapItem)
print("thisisthearray:\(self.matchingItems)")
self.matchingItems = response.mapItems
self.tableView.reloadData()
}
}
}
}
extension LocationSearchTable {
override func tableView(_ tableView: UITableView, didSelectRowAt
indexPath: IndexPath) {
let selectedItem = matchingItems[indexPath.row].placemark
self.dismiss(animated: true, completion: {
self.handleMapSearchDelegate?.dropPinZoomIn(placemark: selectedItem)
})
}
}
I'm not really sure why it won't find the addresses correctly. Maybe I'm not referencing the parseAddress function correctly?
Here's a GitHub link: https://github.com/derekvandermark/VHNavigationAppOfficial-v-0.93