在Swift中将谷歌地图添加到视图中



我面临以下困境。我在我的项目中创建了一个2视图(GMContainer和GMHolder(。我希望GMHolder在他的所有视图中显示谷歌地图。

不幸的是,我只能在整个视图的中间添加一个谷歌地图视图,而不能在我创建的视图中添加。此外,我不知道要将什么添加到mapView作为框架。我尝试创建一个新的CGRect,并添加视图的值,添加我的视图,但id似乎没有起到作用。。。关于如何更改最后一行以使谷歌地图显示在整个GMHolder上,有什么建议吗?

let GMContainer : UIView = {
let view = UIView()
view.backgroundColor = .white
view.layer.cornerRadius = 25
return view
}()
var GMHolder : UIView = {
let view = UIView()
view.layer.cornerRadius = 25
return view
}()
func setupUI() {
GMContainer.translatesAutoresizingMaskIntoConstraints = false
GMHolder.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(GMContainer)
view.addSubview(GMHolder)

GMContainer.topAnchor.constraint(equalTo: categoryBox.bottomAnchor , constant: 32).isActive = true
GMContainer.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 32).isActive = true
GMContainer.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -32).isActive = true
GMContainer.heightAnchor.constraint(equalToConstant: 450).isActive = true
GMHolder.topAnchor.constraint(equalTo: GMContainer.topAnchor , constant: 40).isActive = true
GMHolder.leadingAnchor.constraint(equalTo: GMContainer.leadingAnchor, constant: 24).isActive = true
GMHolder.trailingAnchor.constraint(equalTo: GMContainer.trailingAnchor, constant: -24).isActive = true
GMHolder.heightAnchor.constraint(equalToConstant: 300).isActive = true
//GM View Code
var mapView : GMSMapView

let camera = GMSCameraPosition.camera(withLatitude: 37.321262, longitude: -122.378945, zoom: 10)
mapView = GMSMapView.map(withFrame: CGRect(x: GMHolder.frame.origin.x, y: GMHolder.frame.origin.y , width: 300, height: 300), camera: camera)
//   mapView.center = self.view.center
view.addSubview(mapView)
GMHolder = mapView

模拟器的照片,正如你所看到的,它不局限于视图。

初始化GMHolder内部的所有东西,它似乎起到了作用。

var GMHolder : GMSMapView = {
GMSServices.provideAPIKey(API_KEY)
let camera = GMSCameraPosition.camera(withLatitude: 37.321262, longitude: -122.378945, zoom: 12)
let view = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
view.layer.cornerRadius = 25
view.backgroundColor = .black
return view
}()

相关内容

  • 没有找到相关文章

最新更新