gpt4 book ai didi

ios - 如何在同一个 View Controller 中使用多个搜索 Controller ?

转载 作者:行者123 更新时间:2023-11-28 06:18:14 26 4
gpt4 key购买 nike

在我的应用程序中,我想为 GooglePlace 自动完成使用多个(两个)搜索 Controller 我已经实现了一个搜索 Controller 它工作正常但现在我不知道如何在同一个 View Controller 中实现另一个搜索 Controller 请提供一些知识实现这一点。

这里我给出了实现单个搜索 Controller 的代码。

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
}

最佳答案

您可以通过将 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.

}

关于ios - 如何在同一个 View Controller 中使用多个搜索 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44470896/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com