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

后端 未结 10 918
遇见更好的自我
遇见更好的自我 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:36

    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:

    GMSMapView Mark

    2) Don't forget to connect your view with the outlet in code:

    Connecting view and outlet

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

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

    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.

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

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

    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];
    }
    
    0 讨论(0)
提交回复
热议问题