在 Swift 中监听 InfowWindow 点击

Listen for InfowWindow tap in Swift

我知道可以在标记的信息窗口中捕捉点击。 I followed the documentation here.

都是用Objective C写的,所以我试着把它转换成Swift,这是我的代码:

func mapView(_ mapView: GMSMapView, didTap InfoWindowOfMarker: GMSMarker) {
        print("You tapped infowindow")

    }

但这根本没有被解雇。方法有什么问题?

您需要使用 GMSMapView 的委托以及一些预先设置,请参见下文。

声明使用GMSMapViewDelegate方法并将委托设置为self:

class yourClassName: UIViewController,GMSMapViewDelegate

mapView?.delegate = self

检测信息窗口点击的方法:

func mapView(_ mapView: GMSMapView, didTapInfoWindowOf marker: GMSMarker) {
    print("infowindow tapped")
}

检测点击GMSMarker的方法:

func mapView(mapView: GMSMapView, didTapMarker marker: GMSMarker) -> Bool {
    print("tapped on marker")

    if marker.title == "myMarker"{
        print("handle specific marker")
    }
    return true
}

创建自定义信息窗口的方法:

func mapView(mapView: GMSMapView!, markerInfoWindow marker: GMSMarker!) -> UIView! {
        let infoWindow = Bundle.main.loadNibNamed("nibName", owner: self, options: nil).first as! ClassName
        infoWindow.name.text = "title"
        infoWindow.address.text = "relevant address"
        infoWindow.photo.image = UIImage(named: "imageName")
        return infoWindow
    }