I want to display google maps on a view that's then added to self.view, rather than drawing the map directly on self.view. Therefore, I created a view in storyboard and changed its class to GMSMapView. I also created an outlet connection to that view called gmView.
I am using the following code that does unfortunately not show the map:
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: GMSCameraPosition.camera(withLatitude: 51.050657, longitude: 10.649514, zoom: 5.5))
gmView = mapView
Also, I tried adding the mapView to self.view as a subview, like this:
self.view.addSubview(mapView)
...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?
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() {
super.viewDidLoad()
mapView = GMSMapView.map(withFrame: CGRect(x: 100, y: 100, width: 200, height: 200), camera: GMSCameraPosition.camera(withLatitude: 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 = self.view.center
self.view.addSubview(mapView!)
}
}
Here is the output. mapView is of width = 200 and height = 200 with center as same as self.view

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With