在我的应用程序中,我想使用多个(两个)搜索控制器进行GooglePlace自动完成我已经实现了一个搜索控制器它工作正常但现在我不知道如何在同一视图控制器中实现另一个搜索控制器请提供一些知识实现它。
这里我给出了我实现单一搜索控制器的代码。
var resultsViewController: GMSAutocompleteResultsViewController?
var pickUpSearchController: UISearchController?
var dropSearchController: UISearchController?
let pickUpSearchBarBackView = UIView()
let dropSearchBarBackView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
pickUpSearchBarFunction()
}
override func viewDidLayoutSubviews() {
var pickUpSearchBarFrame = pickUpSearchController?.searchBar.frame
pickUpSearchBarFrame?.size.width = (pickUpSearchController?.searchBar.frame.size.width)!
pickUpSearchController?.searchBar.frame = pickUpSearchBarFrame!
}
func pickUpSearchBarFunction() {
resultsViewController = GMSAutocompleteResultsViewController()
resultsViewController?.delegate = self
pickUpSearchController = UISearchController(searchResultsController: resultsViewController)
pickUpSearchController?.searchResultsUpdater = resultsViewController
if UIScreen.main.bounds.size.width == 320 {
pickUpSearchBarBackView.frame = CGRect(x: 10, y: 75.0, width: 300.0, height: 50.0)
}
else if(UIScreen.main.bounds.size.width == 375)
{
pickUpSearchBarBackView.frame = CGRect(x: 10, y: 75.0, width: 355.0, height: 60.0)
}
else if(UIScreen.main.bounds.size.width == 414)
{
pickUpSearchBarBackView.frame = CGRect(x: 10, y: 75.0, width: 394.0, height: 60.0)
}
pickUpSearchBarBackView.addSubview((pickUpSearchController?.searchBar)!)
//(pickUpSearchController?.searchBar)!.center = self.pickUpSearchBarBackView.center
view.addSubview(pickUpSearchBarBackView)
pickUpSearchController?.searchBar.sizeToFit()
pickUpSearchController?.searchBar.barTintColor = UIColor.white
pickUpSearchController?.hidesNavigationBarDuringPresentation = false
definesPresentationContext = true
navigationController?.navigationBar.isTranslucent = false
pickUpSearchController?.hidesNavigationBarDuringPresentation = false
self.extendedLayoutIncludesOpaqueBars = true
self.edgesForExtendedLayout = .top
}
extension HomeViewController: GMSAutocompleteResultsViewControllerDelegate {
func resultsController(_ resultsController: GMSAutocompleteResultsViewController,
didAutocompleteWith place: GMSPlace) {
pickUpSearchController?.isActive = false
//strForLat = "\(place.coordinate.latitude)"
//strForLong = "\(place.coordinate.longitude)"
let coordinateSearch: CLLocationCoordinate2D? = place.coordinate
print(coordinateSearch!.latitude)
print(coordinateSearch!.longitude)
strForLat = "\(coordinateSearch!.latitude)"
strForLong = "\(coordinateSearch!.longitude)"
globalCamera = GMSCameraPosition.camera(withLatitude: Double(strForLat)!, longitude: Double(strForLong)!, zoom: zoomLevel)
self.googleMapsView.camera = globalCamera
//let marker = GMSMarker()
customerMarker.position = CLLocationCoordinate2DMake(Double(strForLat)!, Double(strForLong)!)
customerMarker.title = place.name
customerMarker.snippet = place.formattedAddress
customerMarker.map = self.googleMapsView
//globalMarker.icon = GMSMarker.markerImage(with: UIColor.blue)
customerMarker.tracksViewChanges = true
self.dismiss(animated: true, completion: nil) // dismiss after select place
pickUpSearchController?.searchBar.text = place.formattedAddress
print("Place name: \(place.name)")
print("Place address: \(place.formattedAddress!)")
print("Place attributions: \(place.attributions)")
//getLatLngForaddress(addressStr: (searchController?.searchBar.text)!)
getLatandLngForPickUpaddress()
}
func resultsController(_ resultsController: GMSAutocompleteResultsViewController,
didFailAutocompleteWithError error: Error){
print("Error: ", error.localizedDescription)
}
// Turn the network activity indicator on and off again.
func didRequestAutocompletePredictions(forResultsController resultsController: GMSAutocompleteResultsViewController) {
UIApplication.shared.isNetworkActivityIndicatorVisible = true
}
func didUpdateAutocompletePredictions(forResultsController resultsController: GMSAutocompleteResultsViewController) {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}
func wasCancelled(_ viewController: GMSAutocompleteViewController) {
self.dismiss(animated: true, completion: nil) // when cancel search
}
}
答案 0 :(得分:1)
您可以通过将resultsController与特定于UISearchController的searchResultsController进行比较来处理它。
extension ViewController: GMSAutocompleteResultsViewControllerDelegate {
func resultsController(_ resultsController: GMSAutocompleteResultsViewController,
didAutocompleteWith place: GMSPlace) {
if resultsController == sourceResultsViewController {
// Do something with the selected place.
} else if resultsController == destinationResultsViewController {
// Do something with the selected place.
}