[英]Is there a way to have InfoWindow open on App load in Google Maps API?
當前,當 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 的子類。
上面的代碼是我認為從更大的代碼庫中提取的相關代碼。 讓我知道是否需要添加任何內容!
任何幫助表示贊賞!
也許這個答案會幫助別人。 問題出在mapView.projection
函數中,它們基本上返回了默認值。 我不完全確定,但似乎當您單擊標記時,mapView 會在背景中更改frame = ( 0 0;)
變量的一部分,因此這導致我更改了mapView
初始化。
所以,而不是...
let camera = GMSCameraPosition.camera(withLatitude: 37, longitude: -77, zoom: 16.0)
mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
我們需要 ...
let camera = GMSCameraPosition.camera(withLatitude: LAT_OF_YOUR_INFOWINDOW,
longitude: LONG_OF_YOUR_INFOWINDOW,
zoom: 16.0)
mapView = GMSMapView.map(withFrame: self.view.bounds, camera: camera)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.