Layering a table view and a google map view iOS Swift

大城市里の小女人 提交于 2019-12-11 19:24:12

问题


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


回答1:


This depends on the order which they have in the view controller's list of subviews. If mapView and tableView are both direct subviews of the root view, you can just call insertSubview:belowSubview:

[self.view insertSubview:mapView belowSubview:tableView];

and vice versa.




回答2:


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


来源:https://stackoverflow.com/questions/31862022/layering-a-table-view-and-a-google-map-view-ios-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!