将自定义信息 Window 添加到 Google 地图

Adding Custom Info Window to Google Maps

我想添加自定义信息 window,以便在有人点击图钉时在 Google 地图中弹出。我已经通过不传递任何数据来隐藏当前信息 window 并且知道函数

func mapView(mapView: GMSMapView!, didTapMarker marker: GMSMarker!) -> Bool {

    return true
}

处理点按图钉时发生的情况。我目前在我的 MapViewController 上有这个自定义 UIView 和属性,并将相应的出口添加到 ViewController 代码中:

我如何实现这个 UIView 以在我点击图钉时弹出?

您应该为注释创建自定义视图,然后return为以下方法创建自定义视图:

func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
 //Create and load your custom view here and then return the view
}

您必须使用 GMSMapViewDelegate 才能将 markerInfoWindow 用于您的 class。

let mapview = GMSMapView.map(withFrame: CGRect.zero, camera: camera)

mapview.delegate = self


func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
    // Get a reference for the custom overlay
    let index:Int! = Int(marker.accessibilityLabel!)
    let customInfoWindow = Bundle.main.loadNibNamed("CustomInfoWindow", owner: self, options: nil)?[0] as! CustomInfoWindow
    customInfoWindow.Timings.text = States[index].time
    customInfoWindow.Title.text = States[index].name
    customInfoWindow.Direction_Button.tag = index
    customInfoWindow.Direction_Button.addTarget(self, action: #selector(GetDirectionsAction(sender:)), for: .touchUpInside)

    return customInfoWindow
}