【问题标题】:Adding Custom Info Window to Google Maps将自定义信息窗口添加到 Google 地图
【发布时间】:2017-09-28 09:40:15
【问题描述】:
我想添加一个自定义信息窗口,以便在有人点击图钉时在 Google 地图中弹出。我已经隐藏了当前的信息窗口,没有传递任何数据并且知道该函数
func mapView(mapView: GMSMapView!, didTapMarker marker: GMSMarker!) -> Bool {
return true
}
处理点击针时发生的情况。我目前在我的 MapViewController 上有这个自定义 UIView 和属性,并将相应的插座添加到 ViewController 代码中:
如何实现这个 UIView 在我点击 pin 时弹出?
【问题讨论】:
标签:
ios
swift
google-maps
uiview
google-maps-markers
【解决方案1】:
您必须使用 GMSMapViewDelegate 才能将 markerInfoWindow 用于您的班级。
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
}
【解决方案2】:
您应该为注释创建一个自定义视图,然后为以下方法返回该自定义视图:
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
//Create and load your custom view here and then return the view
}