所以我试图创建一个UISearchController
来使用Google Places SDK在地图上搜索位置。我正在使用来自文档link的尽可能多的代码,但不适用于与搜索栏进行交互。
我尝试搜索答案。许多答案包括将segue更改为“ show”或添加以下行:
self.definesPresentationContext = false
但是,这两种修复方法都无法满足我的需求。
下面的完整代码:
import UIKit
import CoreLocation
import GoogleMaps
import GooglePlaces
class AddressViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var nextButton: UIBarButtonItem!
let locationManager = CLLocationManager()
var mapView: GMSMapView!
var resultsViewController: GMSAutocompleteResultsViewController?
var searchController: UISearchController?
var resultView: UITextView?
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
enableBasicLocationServices()
self.definesPresentationContext = false
let camera = GMSCameraPosition.camera(withLatitude: 39.95, longitude: -75.16, zoom: 10.0)
mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
self.view = mapView
navigationController?.navigationBar.isTranslucent = false
searchController?.hidesNavigationBarDuringPresentation = false
// This makes the view area include the nav bar even though it is opaque.
// Adjust the view placement down.
self.extendedLayoutIncludesOpaqueBars = true
self.edgesForExtendedLayout = .top
resultsViewController = GMSAutocompleteResultsViewController()
resultsViewController?.delegate = self
searchController = UISearchController(searchResultsController: resultsViewController)
searchController?.searchResultsUpdater = resultsViewController
let subView = UIView(frame: CGRect(x: 0, y: 65.0, width: 350.0, height: 45.0))
subView.addSubview((searchController?.searchBar)!)
view.addSubview(subView)
searchController?.searchBar.sizeToFit()
searchController?.hidesNavigationBarDuringPresentation = false
// searchController?.isActive = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func enableBasicLocationServices() {
let status = CLLocationManager.authorizationStatus()
// Check initial authorization
if status == .notDetermined {
locationManager.requestWhenInUseAuthorization()
return
}
// Handle location serviced denied
if status == .denied || status == .restricted {
let alert = UIAlertController(title: "Location Services Disabled", message: "Please enable Location Services in Settings", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(okAction)
present(alert, animated: true, completion: nil)
return
}
if status == .authorizedWhenInUse {
print("Status Authorized!")
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
locationManager.startUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
locationManager.startUpdatingLocation()
mapView.isMyLocationEnabled = true
mapView.settings.myLocationButton = true
}
else {
locationManager.stopUpdatingLocation()
mapView.isMyLocationEnabled = false
mapView.settings.myLocationButton = false
}
}
}
// Handle the user's selection.
extension AddressViewController: GMSAutocompleteResultsViewControllerDelegate {
func resultsController(_ resultsController: GMSAutocompleteResultsViewController,
didAutocompleteWith place: GMSPlace) {
searchController?.isActive = false
// Do something with the selected place.
print("Place name: \(place.name)")
print("Place address: \(place.formattedAddress)")
print("Place attributions: \(place.attributions)")
searchController?.isActive = true
}
func resultsController(_ resultsController: GMSAutocompleteResultsViewController,
didFailAutocompleteWithError error: Error){
// TODO: handle the 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
}
}
大多数代码都用于处理场所和地图SKD的委托方法,但是,如果与问题有关,我想将其包括在内,因为这几乎是文档中的逐字记录。
任何帮助表示感谢,非常感谢!