Google Maps GMSMapView on custom UIView

2020-02-23 07:36发布

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?

7条回答
贼婆χ
2楼-- · 2020-02-23 08:27

You can position a blank UIView in your layout, and set its class in the inspector as a GMSMapView. You won't need to programmatically instantiate it.

Then just get a reference via outlet to the view and set your coordinates/camera.

查看更多
登录 后发表回答