Search code examples
iosswiftgoogle-maps-sdk-ios

Layering a table view and a google map view iOS Swift


I'm trying to set up a map search using the google maps sdk in ios. So far, I have the autocompleted search working correctly and when I search, the results show up in a tableview. I want to be able to click on a cell in the table view and reveal the map, then show the table again when the user touches back into the search box. My strategy to do this is to layer the mapview and the table view in the same view Controller, and show/hide or push whichever view to the top. However, because the google map is not part of mapkit, I'm not sure that I can manipulate it like this. Please let me know if you know an easy way to do this. Setting the view to tableview also results in a black screen

mapView.hidden just reveals a black screen

 override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.dataSource = self
    self.tableView.delegate = self

    self.resultSearchController = ({
        let controller = UISearchController(searchResultsController: nil)
        controller.searchResultsUpdater = self
        controller.dimsBackgroundDuringPresentation = false
        controller.searchBar.sizeToFit()

        self.tableView.tableHeaderView = controller.searchBar

        return controller
    })()
    view.bringSubviewToFront(self.tableView)
    placesClient = GMSPlacesClient()
    var camera = GMSCameraPosition.cameraWithLatitude(-33.86,
        longitude: 151.20, zoom: 6)
    var mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
    //mapView.hidden = true

    mapView.myLocationEnabled = true
    self.view = mapView
    self.view = self.tableView
    var marker = GMSMarker()
    marker.position = CLLocationCoordinate2DMake(-33.86, 151.20)
    marker.title = "Sydney"
    marker.snippet = "Australia"
    marker.map = mapView

}

I am using the google maps cocoapod


Solution

  • I found out that I had to add my own subview, change the class of that subview to GMSMapview and assign the map to that subview.

    var camera = GMSCameraPosition.cameraWithLatitude(-33.86,
            longitude: 151.20, zoom: 6)
    
        var mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
        mapView.myLocationEnabled = true
    
        self.mapSubView.camera = camera
        self.mapSubView = mapView