ksa_coder
ksa_coder

Reputation: 1403

Change Google Map size iOS Swift

I can't find anything on this so I am asking. when I use the code provided by Google on its website, I get a map in the controller that is full screen. I want to be able to show it within a container (fixed width & height).

I am using swift and would appreciate your help. Below is the code I extracted from google:

Override func viewDidLoad() {
        super.viewDidLoad()

        var camera = GMSCameraPosition.cameraWithLatitude(-33.86,
            longitude: 151.20, zoom: 2)
        var mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
        mapView.myLocationEnabled = true
        self.view = mapView

        var marker = GMSMarker()
        marker.position = CLLocationCoordinate2DMake(-33.86, 151.20)
        marker.title = "Sydney"
        marker.snippet = "Australia"
        marker.map = mapView

        // Do any additional setup after loading the view.
    }

Upvotes: 2

Views: 3881

Answers (1)

IxPaka
IxPaka

Reputation: 2008

Your map is going to be as big as the view you are using. You have 2 options.

  • 1
    Create subview of specific size and use that view for your GMSMapView, same way you do.
  • 2
    Instead of assigning your GMSMapView to some view use

    view.insertSubview(mapView, atIndex:0)
    

    Remember that you have to create a frame for your GMSMapView in this case

    var mapView = GMSMapView.mapWithFrame(CGRectMake(...))
    

    Good thing about this is you can add other components above your map if you want to(some buttons, labels, etc.), thats not possible with first approach.

EDIT

This is the way I do it using autolayout. I created some view in storyboard, gave it constraints so it would resize according to screen size. Then I initialize my map view and use size of my autoresizing view and insert my mapView to this view.

mapView_ = [[GMSMapView alloc] initWithFrame:self.viewForMap.bounds];
mapView_.delegate = self;
mapView_.camera = camera;
[self.viewForMap insertSubview:mapView_ atIndex:0];

Yes I know this is Objective-C, but this is just an example.

Upvotes: 2

Related Questions