自定义 UIView 上的 Google Maps GMSMapView [英] Google Maps GMSMapView on custom UIView
问题描述
我想在添加到 self.view
的视图上显示谷歌地图,而不是直接在 self.view
上绘制地图.因此,我在故事板中创建了一个视图并将其类更改为 GMSMapView
.我还为该视图创建了一个名为 gmView
的插座连接.
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
此外,我尝试将 mapView
添加到 self.view
作为子视图,如下所示:
Also, I tried adding the mapView
to self.view
as a subview, like this:
self.view.addSubview(mapView)
...并插入它:
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?
推荐答案
如果你想在view
加载后添加一个mapView
,那么你需要创建GMSMapView
的一个对象.所以打破你的 mapView
的出口,因为它会动态创建.
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!)
}
}
这是输出.mapView
的宽度 = 200,高度 = 200,中心与 self.view
Here is the output. mapView
is of width = 200 and height = 200 with center as same as self.view
这篇关于自定义 UIView 上的 Google Maps GMSMapView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!