Search code examples
swiftgoogle-mapsuiviewsubviewgoogle-maps-sdk-ios

Not being able to size Google Map in UIView


I'm using Google Maps SKD and I'm trying to make a map fits in a UIView. Firstly, I've added a full screen UIView which displays a GMSPanoramaView in my app. It works great, here is variables declaration:

 @IBOutlet weak var streetViewer: UIView!
 @IBOutlet weak var gmapViewer: UIView!

 var panoView: GMSPanoramaView!
 var mapView: GMSMapView!

Then in viewDiDLoad() I create the panorama and add it as a subview of streetViewer:

 panoView = GMSPanoramaView(frame: CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height))
 panoView.delegate = self
 panoView.moveNearCoordinate(CLLocationCoordinate2D(latitude: -33.732, longitude: 150.312))
 streetViewer.addSubview(panoView)
 streetViewer.sendSubviewToBack(panoView)

Now I'm trying to do the same with a UIView which covers about half of the screen and displays mapView over the layer of panoView. So I create the map, add it as a subview of gmapViewer and finally add it too as a subview and move it on top of panoView:

 mapView = GMSMapView(frame: CGRectMake(0, 0, gmapViewer.bounds.size.width, gmapViewer.bounds.size.height))
 mapView.delegate = self
 gmapViewer.addSubview(mapView)
 streetViewer.addSubview(gmapViewer)
 panoView.sendSubviewToBack(gmapViewer)

I've added constrainst to all UIView. I've also added some paper folding animation to gmapViewer and all work great, apart from the map which does not fill gmapViewer. I think the problem stands in mapView = GMSMapView(frame: CGRectMake(0, 0, gmapViewer.bounds.size.width, gmapViewer.bounds.size.height)) but I can't understand what does not work since it worked for panoView.


Solution

  • Posting this after almost 5 years because it bugs me that this question is still without an answer, i think i improved enough a iOS programmer to be able to answer my own question and i hope this helps out someone else.

    My mistake was that i messed out trying to mix constraints set programmatically and by storyboard, not sure why i didn't simply set mapView with frame CGRect(gmapViewer.bounds.size.width/2, 0, gmapViewer.bounds.size.width/2, gmapViewer.bounds.size.height).

    Anyway, the code down below displays a full screen Google Street View, covered by a half screen sized Google Map View.

    private let mapView = GMSMapView()
    private let streetView = GMSPanoramaView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addSubview(streetView)
        self.view.addSubview(mapView)
        streetView.moveNearCoordinate(CLLocationCoordinate2D(latitude: 37.3317134, longitude: -122.0307466))
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        streetView.frame = CGRect(x: 0,
                                  y: 0,
                                  width: view.bounds.size.width,
                                  height: view.bounds.size.height)
        mapView.frame = CGRect(x: view.bounds.size.width/2,
                               y: 0,
                               width: view.bounds.size.width/2,
                               height: view.bounds.size.height)
    }