From 5fb58bf1c9c46e33a37a04c36c55ed3d70ea8aa8 Mon Sep 17 00:00:00 2001 From: cindy-x-liang <67083541+cindy-x-liang@users.noreply.github.com> Date: Sun, 4 Aug 2024 19:21:01 -0600 Subject: [PATCH] add route filtering --- TCAT/Controllers/HomeMapViewController.swift | 1 + .../HomeOptionsCardViewController.swift | 1 - TCAT/Controllers/SearchResultsViewController.swift | 1 + TCAT/Models/SearchManager.swift | 14 +++++++++++++- 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/TCAT/Controllers/HomeMapViewController.swift b/TCAT/Controllers/HomeMapViewController.swift index 23e24a17..1739c44e 100644 --- a/TCAT/Controllers/HomeMapViewController.swift +++ b/TCAT/Controllers/HomeMapViewController.swift @@ -36,6 +36,7 @@ class HomeMapViewController: UIViewController { private let loadingIndicatorSize = CGSize.init(width: 40, height: 40) private let userDefaults = UserDefaults.standard + override func viewDidLoad() { super.viewDidLoad() diff --git a/TCAT/Controllers/HomeOptionsCardViewController.swift b/TCAT/Controllers/HomeOptionsCardViewController.swift index 0823cd3e..a9e56051 100644 --- a/TCAT/Controllers/HomeOptionsCardViewController.swift +++ b/TCAT/Controllers/HomeOptionsCardViewController.swift @@ -300,7 +300,6 @@ class HomeOptionsCardViewController: UIViewController { } // MARK: - Get Search Results - /// Get Search Results @objc func getPlaces(timer: Timer) { if let userInfo = timer.userInfo as? [String: String], diff --git a/TCAT/Controllers/SearchResultsViewController.swift b/TCAT/Controllers/SearchResultsViewController.swift index 73633253..8bb2d695 100755 --- a/TCAT/Controllers/SearchResultsViewController.swift +++ b/TCAT/Controllers/SearchResultsViewController.swift @@ -153,6 +153,7 @@ class SearchResultsViewController: UIViewController { } @objc private func getPlaces(timer: Timer) { + print("here") if let userInfo = timer.userInfo as? [String: String], let searchText = userInfo["searchText"], !searchText.isEmpty { diff --git a/TCAT/Models/SearchManager.swift b/TCAT/Models/SearchManager.swift index 2d46a866..71e98fbf 100644 --- a/TCAT/Models/SearchManager.swift +++ b/TCAT/Models/SearchManager.swift @@ -25,6 +25,9 @@ class SearchManager: NSObject { private let networking: Networking = URLSession.shared.request private let searchCompleter = MKLocalSearchCompleter() private var searchResults = [MKLocalSearchCompletion]() + + private let gshLat = 42.442558 + private let gshLong = -76.485336 override private init() { super.init() @@ -41,6 +44,13 @@ class SearchManager: NSObject { ) } } + private func sortLocations(_ s1: Place, _ s2: Place) -> Bool { + let s1Check = pow((s1.latitude-(self.gshLat)),2.0) + pow((s1.longitude-(self.gshLong)),2.0) + + let s2Check = pow((s2.latitude-(self.gshLat)),2.0) + pow((s2.longitude-(self.gshLong)),2.0) + return s1Check < s2Check + } + func performLookup(for query: String, completionHandler: @escaping SearchManagerCallback) { getAppleSearchResults(searchText: query).observe { [weak self] result in @@ -55,7 +65,9 @@ class SearchManager: NSObject { // If the list of Apple Places for this query already exists in // server cache, no further work is needed if let applePlaces = response.data.applePlaces { - let searchResults = applePlaces + busStops + let updatedApplePlaces = applePlaces.sorted(by: self.sortLocations) + + let searchResults = updatedApplePlaces + busStops completionHandler(searchResults, nil) } else { // Otherwise, we need to perform the Apple Places lookup locally