Cannot put a google maps GMSMapView in a subview of main main view?
Based on other answers, here are three ways that actually work.
- Put a view on the XIB and change its class to GMSMapView in XIB builder. See *map1 below. Or...
- Code it all. Add the map as a subview of the main view. See *map2 below. Or...
- 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];}
Have checked out and get working in Swift 2.0 all of the three options provided by cloudsurfin. Translating his answer for those who face the same problem in Swift 2.0:
Option1 - Put a view on the XIB and change its class to GMSMapView in XIB builder.
Option2 - Code it all. Add the map as a subview of the main view.
Option3 - Add the map as a GMSMapView subview of already existing UIView subview.
Explanation of preparations in XIB for Option1:
1) Set a class for your view:
2) Don't forget to connect your view with the outlet in code:
import UIKitimport GoogleMapsclass MapViewController: UIViewController { @IBOutlet weak var map1 : GMSMapView! @IBOutlet weak var map3 : UIView! override func viewDidLoad() { super.viewDidLoad() let camera = GMSCameraPosition.cameraWithLatitude(53.9, longitude: 27.5667, zoom: 6) // Option 1. view on XIB is class GSMMapView self.map1.camera = camera; // Option 2. add a map as a subview let map2 = GMSMapView.mapWithFrame(CGRectMake(0, 64, 320, 460), camera:camera) self.view.addSubview(map2) // Option 3. add a map to a subview of UIView class that is already on the XIB let map3 = GMSMapView.mapWithFrame(self.plainViewOnXIBHoldsMap3.bounds, camera:camera) self.plainViewOnXIBHoldsMap3.addSubview(map3) }}
Hope this note helps someone :)
My suggestions:
- Link the UIView from your storyboard to your header file as a UIView instance variable, not a GMSMapView
- Change the mapWithFrame:camera: method to be set to your linked UIView's bounds
- At the end of the viewDidLoad: method, set the UIView instance variable to your GMSMapView, or add a subview. I've had trouble with this as well, and fooling around with this usually will work.
.h
@interface MapViewController: UIViewController <CLLocationManagerDelegate>{ IBOutlet UIView *mapViewOnSreen;}
.m
- (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:mapViewOnScreen.bounds camera:camera]; _mapView.myLocationEnabled = YES; mapViewOnScreen = _mapView //OR [self.view addSubview:_mapView]; //OR [mapViewOnScreen addSubview:_mapView]; <--This worked for me}
*Edit: I created a small UIView inside the main view using IB. I followed the steps listed and I was able to view the map inside the small UIView when I set [mapViewOnScreen addSubview:_mapView];