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

AndreaZ picture AndreaZ · Mar 14, 2013 · Viewed 35.9k times · Source

I'm struggling with this problem! I want to add a google maps GMSMapView into a UIView that is only a portion of the main UIView of my ViewController.

It should be simple... I created with the storyboard a UIView of the size I want and put it in the main UIView.

Snippet from Interface file:

@interface MapViewController : UIViewController <CLLocationManagerDelegate>

@property(nonatomic) IBOutlet GMSMapView *mapView;

With the mapView linked with this UIView inside the main UIView.

What I do in the implementation:

@synthesize mapView = _mapView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.8683
                                                            longitude:151.2086
                                                                 zoom:10];
    _mapView = [GMSMapView mapWithFrame:_mapView.bounds camera:camera];
    _mapView.myLocationEnabled = YES;

}

This should work, shouldn't it? What I get is that my internal UIView is empty.

If I instantiate a map following google's starting guide Google Map Integration Document, it works but it assigns the map to the MAIN UIView and thats a different thing.

I tried even changing the class of my mapView in the storyboard from UIView to GMSMapView. The map is showed in the inner view but it is initialized in a strange way making

  1. The system throwing an error saying

"Failed to make complete frame buffer"

and slowing down a lot the loading of the view (it takes 2-3 seconds on the simulator)

  1. The map not responding in any camera change done in the viewDidLoad method.

Any suggestions?

I read some posts here on StackOverflow but couldn't find a valid solution :(

Answer

cloudsurfin picture cloudsurfin · Jul 19, 2013

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];
}