Linus G. Linus G. - 1 year ago 190
iOS Question

Google Maps GMSMapView on custom UIView

I want to display google maps on a view that's then added to

, rather than drawing the map directly on
. Therefore, I created a view in storyboard and changed its class to
. I also created an outlet connection to that view called

I am using the following code that does unfortunately not show the map:

let mapView =, camera: 51.050657, longitude: 10.649514, zoom: 5.5))
gmView = mapView

Also, I tried adding the
as a subview, like this:


...and inserting it:

self.view.insertSubview(mapView, at: 0)

Note that I'm using Auto Layout if that changes anything.

None of these approaches seem to work for me.

Any ideas?

Answer Source

If you want to add a mapView after the loading of the view, then you need to create an object of GMSMapView. So break the outlets of your mapView since it will be created dynamically.

import UIKit
import GoogleMaps

class MapViewController: UIViewController {

    //Take a Google Map Object. Don't make outlet from Storyboard, Break the outlet of GMSMapView if you made an outlet
    var mapView:GMSMapView?

    override func viewDidLoad() {


        mapView = CGRect(x: 100, y: 100, width: 200, height: 200), camera: 51.050657, longitude: 10.649514, zoom: 5.5))

        //so the mapView is of width 200, height 200 and its center is same as center of the self.view
        mapView?.center =



Here is the output. mapView is of width = 200 and height = 200 with center as same as self.view

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download