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

后端 未结 10 917
遇见更好的自我
遇见更好的自我 2020-11-28 06:50

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 m

相关标签:
10条回答
  • 2020-11-28 07:24

    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)
    }
    
    0 讨论(0)
  • 2020-11-28 07:25

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

    [super loadView];
    
    0 讨论(0)
  • 2020-11-28 07:29
    (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;
    }
    
    0 讨论(0)
  • 2020-11-28 07:30

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

    0 讨论(0)
  • 2020-11-28 07:30

    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;
    
    0 讨论(0)
  • 2020-11-28 07:32

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

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

    0 讨论(0)
提交回复
热议问题