UISearchController with Google Maps using Swift

I am trying to create a screen on which a Google map will be displayed, covering the entire screen, and a button at the top showing the selected address (where the marker indicates). And I want, when a user clicks on this button, to present a UISearchBar where he can search for locations using the Google Places API.

I ran the following tutorial: https://www.youtube.com/watch?v=gRQUoHleCGM

Unfortunately, in my case, the search bar does not appear (although the background is darkened when I click the button).

Here are some screenshots before you click and after.

enter image description here

enter image description here

And this is the code of my view controller.

class ChooseLocationViewController: UIViewController, GMSMapViewDelegate, UISearchBarDelegate, UISearchControllerDelegate {

    let geocoder = GMSGeocoder()
    var mapView: GMSMapView = GMSMapView.mapWithFrame(CGRectZero, camera:GMSCameraPosition.cameraWithLatitude(51.515339, longitude: -0.141838, zoom: 16))
    var addressButton: UIButton = UIButton()

    let locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        mapView.delegate = self

        let marker = UIImageView(frame: CGRect(x: 20, y: 20, width: 50, height: 50))
        marker.image = UIImage(named: "default-marker")
        marker.center.x = self.view.center.x
        marker.center.y = self.view.center.y - (self.navigationController?.navigationBar.frame.size.height)!

        addressButton = UIButton(frame: CGRect(x: 20, y: 20, width: self.view.frame.size.width - 25, height: 47))
        addressButton.center.x = self.view.center.x
        addressButton.backgroundColor = UIColor(red: 248/255, green: 248/255, blue: 248/255, alpha: 1.0)
        addressButton.layer.borderColor = UIColor(red: 178/255, green: 178/255, blue: 178/255, alpha: 1.0).CGColor
        addressButton.layer.borderWidth = 1.0
        addressButton.layer.cornerRadius = 5
        addressButton.setTitleColor(UIColor(red: 68/255, green: 68/255, blue: 68/255, alpha: 1.0), forState: .Normal)
        addressButton.titleLabel?.font = UIFont.systemFontOfSize(13)
        addressButton.addTarget(self, action: "showLocationSearch", forControlEvents: .TouchDown)

        self.view = mapView
        self.view.addSubview(marker)
        self.view.addSubview(addressButton)
    }

    func showLocationSearch() {
        let searchController = UISearchController(searchResultsController: nil)
        searchController.delegate = self
        searchController.searchBar.delegate = self
        self.presentViewController(searchController, animated: true, completion: nil)
    }

    func willPresentSearchController(searchController: UISearchController) {
        self.navigationController?.extendedLayoutIncludesOpaqueBars = true
    }

    func willDismissSearchController(searchController: UISearchController) {
        self.navigationController?.extendedLayoutIncludesOpaqueBars = false
    }

    func mapView(mapView: GMSMapView, idleAtCameraPosition position: GMSCameraPosition) {
        let coordinate = position.target
        geocoder.reverseGeocodeCoordinate(coordinate) { response, error in
            if let address = response?.firstResult() {
                if let lines = address.lines {
                    if (lines.count > 0) {
                        self.addressButton.setTitle(lines[0], forState: .Normal)
                    }
                }
            }
        }
    }
}

, , extendedLayoutIncludesOpaqueBars, , , , , . ( ), .

? - ?

+4

All Articles