Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
381 views
in Technique[技术] by (71.8m points)

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

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 :(

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...