问题
I want to add a custom info window to pop up in Google Maps whenever someone taps a pin. I've already hidden the current info window by not passing it any data and know that the function
func mapView(mapView: GMSMapView!, didTapMarker marker: GMSMarker!) -> Bool {
return true
}
Handles what happens when a pin is tapped. I currently have this custom UIView and attributes on my MapViewController and the corresponding outlets added to the ViewController code:
How would I implement this UIView to pop up whenever I tap a pin?
回答1:
you have to use GMSMapViewDelegate to use markerInfoWindow to your 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
}
回答2:
You should create a custom view for the annotation and then return that custom view for the following method:
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
//Create and load your custom view here and then return the view
}
来源:https://stackoverflow.com/questions/43711217/adding-custom-info-window-to-google-maps