Cannot put a google maps GMSMapView in a subview of main main view?

Based on other answers, here are three ways that actually work.

  1. Put a view on the XIB and change its class to GMSMapView in XIB builder. See *map1 below. Or…
  2. Code it all. Add the map as a subview of the main view. See *map2 below. Or…
  3. Add the map as a subview of another subview already in the XIB with an outlet. *map3 below.

.h

@interface GPViewController : UIViewController
@property (strong, nonatomic) IBOutlet GMSMapView *map1;
@property (strong, nonatomic) IBOutlet UIView *plainViewOnXIBHoldsMap3;
@end

.m

- (void)viewDidLoad{
    [super viewDidLoad];
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.0
                                                        longitude:151.20
                                                             zoom:6];

    /* Option 1. view on XIB is class GSMMapView */
    self.map1.camera = camera;

    /* Option 2. add a map as a subview */
    GMSMapView *map2 = [GMSMapView mapWithFrame:CGRectMake(0, 0, 100, 100) camera:camera];
    [self.view addSubview:map2];

    /* Option 3. add a map to a subview already on the XIB */
    GMSMapView *map3 = [GMSMapView mapWithFrame:self.plainViewOnXIBHoldsMap3.bounds camera:camera];
    [self.plainViewOnXIBHoldsMap addSubview:map3];
}

Leave a Comment