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

独自空忆成欢 提交于 2019-11-26 21:59:10

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

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 UIKit
import GoogleMaps

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

  1. Link the UIView from your storyboard to your header file as a UIView instance variable, not a GMSMapView
  2. Change the mapWithFrame:camera: method to be set to your linked UIView's bounds
  3. 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];

you can do it through IB

.h
@property (weak, nonatomic) IBOutlet GMSMapView *theMapView;

.m
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.868
                                                        longitude:151.2086
                                                             zoom:12];

//This next line is not needed if you have a GMSMapView outlet
//self.theMapView = [GMSMapView mapWithFrame:self.theMapView.frame camera:camera];
self.theMapView.camera=camera;
self.theMapView.mapType = kGMSTypeSatellite;
self.theMapView.delegate=self;

I was getting the black screen just because i forgot to add this line:

[super loadView];

I spent about a half hour trying to get this to work and then I figured out that I had it in the wrong method. The google getting started example tells you to put it in - (void)loadView but if you do that you get a black screen or a full screen map. You need to put it in - (void)viewDidLoad. Then it works.

For Swift 3 and if you sub class the GMSMapView

  1. Add UIView into UIViewController in story board

  1. Change class in to GMSMapView instead UiView

  1. Create reference in UIViewController class

    @IBOutlet weak var mapView: GMSMapView!
    
  2. As you are subclassing you do not need use that GMSMapView.map(withFrame: CGRect.zero, camera: camera) mentioned in the docs. Just load map as follows

        let camera = GMSCameraPosition.camera(withLatitude: 51.50, longitude: -0.076, zoom: 10.0)
        mapView.camera = camera
    
  3. In order to add marker

    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: 51.50, longitude: -0.076)
    marker.title = "London"
    marker.snippet = "UK"
    marker.map = mapView
    
  4. Together as a function

    override func viewDidLoad() {
    super.viewDidLoad()
    
      load()
    }
    
    func load() {
      //map
      let camera = GMSCameraPosition.camera(withLatitude: 51.50, longitude: -0.076, zoom: 10.0)
      mapView.camera = camera
    
      //marker
      let marker = GMSMarker()
      marker.position = CLLocationCoordinate2D(latitude: 51.50, longitude: -0.076)
      marker.title = "London"
      marker.snippet = "UK"
      marker.map = mapView
    }
    

set the UIVIEW class to be GMSMapView in the identity inspector.

then make an IBOutlet from it :D and hola it is work :P

Urvish Modi
(void)viewDidLoad {
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
                                                        longitude:151.20
                                                             zoom:14];

    GMSMarker *marker = [[GMSMarker alloc] init];
    marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
    marker.title = @"Sydney";
    marker.snippet = @"Australia";
    marker.map = self.mapContainerView;

    //set the camera for the map
    self.mapContainerView.camera = camera;

    self.mapContainerView.myLocationEnabled = YES;
}

Following the google tutorial for Swift in Sep-2017

I fixed by putting the code in viewDidLoad like this

var mapView: GMSMapView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Create a GMSCameraPosition to display the initial center of the map
    let camera = GMSCameraPosition.camera(withLatitude: 23.885942, longitude: 45.079162, zoom: 5.1)

    // Set the frame size , don't forget to replace "<CustomFrame>" with the required frame size
    mapView = GMSMapView.map(withFrame: "<CustomFrame>", camera: camera)

    // Add the map to any view here 
    view.addSubview(mapView)  //or customView.addSubview(mapView)
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!