【问题标题】:Is there a way to have InfoWindow open on App load in Google Maps API?有没有办法在 Google Maps API 中的 App 加载时打开 InfoWindow?
【发布时间】: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


【解决方案1】:

也许这个答案会对其他人有所帮助。问题出在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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-09
    • 2010-12-24
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多