I've made a custom view to be shown when a user taps on marker on google map. So I've wrote the delegate method markerInfoWindow
as:
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
let infoWindow = Bundle.main.loadNibNamed("emergencyInfo", owner: self.view, options: nil)!.first! as! emergencyInfo
infoWindow.frame = CGRect(x: 0, y: 0, width: 200, height: 110)
infoWindow.titleOfCenter.text = marker.title
infoWindow.addressOfCenter.text = marker.snippet
infoWindow.callNowButton.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
return infoWindow
}
and buttonTapped
fuction is implemented as
@objc func buttonTapped(sender: UIButton) {
print("Yeah! Button is tapped!")
}
but the problem is that it is not going inside the function.
UPDATE
Based on the answer I started following this tutorial so this is what I've implemented:
First I have declared these two variables:
var tappedMarker = GMSMarker()
var infoWindow = emergencyInfo(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
Then in didTap
I did this:
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
let location = CLLocationCoordinate2D(latitude: marker.position.latitude, longitude: marker.position.longitude)
tappedMarker = marker
infoWindow.removeFromSuperview()
infoWindow = emergencyInfo(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
infoWindow.center = mapView.projection.point(for: location)
infoWindow.callNowButton.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside) //getting error on this line
self.view.addSubview(infoWindow)
return false
}
And finally I change the markerInfoWindow
method to:
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
return UIView()
}
Now when I run the code I get error on the line where I'm setting #selector(buttonTapped)
fatal error: unexpectedly found nil while unwrapping an Optional value
Based on some research I found that this error is occuring for the left hand side i.e. infoWindow.callNowButton
because I have a label infoWindow.titleOfCenter.text
for which I've just given "Title"
at the moment and it crashes with the same error on it.