Pangu
Pangu

Reputation: 3819

Why doesn't Google Maps stretch to fit my UIView?

I have created a UIView that I would like to put google maps in. However, when I add GMSMapview to my UIView, the bottom portion of the GMSMapview does not extend to fit the UIView. I can still see the gray part of my UIVIew.

Why is that?

enter image description here

- (void)viewDidLoad 
{
    // Create a GMSCameraPosition that tells the map to display the
    // coordinate -33.86,151.20 at zoom level 6.
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.868
                                                            longitude:151.2086
                                                                 zoom:6];
    GMSMapView *mapView = [GMSMapView mapWithFrame:self.googleMapView.bounds camera:camera];



    [self.googleMapView addSubview:mapView];
}

Upvotes: 1

Views: 1360

Answers (3)

Add the layout in side viewDidAppear

    override func viewDidAppear(_ animated: Bool){
        super.viewDidAppear(animated) 
        //google map setup here
    }

Upvotes: 0

scottysseus
scottysseus

Reputation: 2010

If you are having this issue on Xcode 7, what worked for me (the accepted answer wasn't applicable to me since I am developing in Swift) was checking the 'Autoresize Subviews' box for both the GMSMapView and its parent (which for me was a UIStackView).

Upvotes: 0

Lyndsey Scott
Lyndsey Scott

Reputation: 37290

I suspect your interface isn't necessarily sized for an iPhone 6 so when you set the mapView frame in viewDidLoad, although it initially fits inside your googleMapView, after auto layout occurs, the googleMapView stretches to fit the screen and the mapView stays the same size, which is too small.

To fix this, I suggest moving your code to viewDidLayoutSubviews: so your mapView frame is set after the googleMapView has stretched to fill the screen, ex:

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.868
                                                            longitude:151.2086
                                                                 zoom:6];
    GMSMapView *mapView = [GMSMapView mapWithFrame:self.googleMapView.bounds camera:camera];
    [self.googleMapView addSubview:mapView];
}

Upvotes: 10

Related Questions