【发布时间】:2020-06-11 19:58:57
【问题描述】:
目前,当 MapView 委托调用 didTap 协议时,我的应用程序会打开一个自定义信息窗口。然后这会调用一个函数来初始化下面显示的信息窗口的所需方面...
var tappedOverlay : GMSOverlay?
var customInfoWindow : CustomInfoWindow?
var mapView: GMSMapView!
override func viewDidLoad(){
super.viewDidLoad()
self.customInfoWindow = CustomInfoWindow().loadView()
let camera = GMSCameraPosition.camera(withLatitude: 37, longitude: -77, zoom: 16.0)
mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
mapView.delegate = self
self.view = mapView
//this is where i attempt to force a infoWindow to open
tappedOverlay = overlay
initInfoWindow(overlay: overlay)
}
fun initInfoWindow(overlay: GMSOverlay){
mapView.animate(toLocation: position!)
let point = mapView.projection.point(for: position!)
let newPoint = mapView.projection.coordinate(for: point)
let camera = GMSCameraUpdate.setTarget(newPoint)
mapView.animate(with: camera)
let background = UIColor.infoWindowBackground
customInfoWindow?.layer.backgroundColor = background.cgColor
customInfoWindow?.layer.cornerRadius = 8
customInfoWindow?.center = mapView.projection.point(for: position!)
customInfoWindow?.center.y -= 100
customInfoWindow?.CustomWindowLabel.text = overlay.title
customInfoWindow?.CustomWindowSubLabel.lineBreakMode = .byWordWrapping
customInfoWindow?.CustomWindowSubLabel.numberOfLines = 0
mapView.addSubview(customInfoWindow!)
}
func mapView(_ mapView: GMSMapView, didTap Overlay: GMSOverlay){
initInfoWindow(overlay: overlay)
}
现在我想让应用在加载时自动打开某个信息窗口。
当我尝试通过调用 viewDidLoad() 中的函数来手动显示加载时的 infoWindow 时,我必须注释掉 mapView.animate(with: camera) 行,否则地图图块将永远无法加载。有人向我指出,mapView 方法在调用它们时有一些关于覆盖的“背景”信息,所以我添加了tappedOverlay = overlay 行,这在应用程序加载时显示的 infoWindow 的那部分接近。但是,此信息窗口和所有其他“常规”信息窗口都没有背景,也没有以该点为中心,并且无法与之交互。
infoWindow 是从 XIB 文件加载的 UIView 的子类。
上面的代码是我认为从更大的代码库中提取的相关代码。让我知道是否需要添加任何内容!
感谢任何帮助!
【问题讨论】:
-
请分享更多代码以获得更好的解释
标签: ios swift xcode addsubview gmsmapview