Cant interact with custom infowindow in google map

2019-06-07 02:06发布

问题:

I have a google map and I've created a custom info window for my markers. I've put a couple buttons on the window, but I can't interact with them for the life of me. Here is my custom view:

class MarkerWindowView: UIView, UIGestureRecognizerDelegate {

//@IBOutlet weak var thumbNail: UIImageView!
@IBOutlet weak var userName: UILabel!
@IBOutlet weak var videoLocation: UILabel!
@IBOutlet weak var tags: UILabel!


override func awakeFromNib() {
println("awake From Nib")
    self.userInteractionEnabled = true
    print(self.userInteractionEnabled)
    /*let recognizer = UITapGestureRecognizer(target: self, action:Selector("handleTap:"))
    recognizer.delegate = self
    /*for test in layer.sublayers {
        println(test)
    }*/*/
}

@IBAction func playVideoPressed(sender: AnyObject) {
    println("play video")
}
@IBAction func markerWindowViewWasTapped(sender: UITapGestureRecognizer) {
    println("playvideo")
}
/*func handleTap(recognizer: UITapGestureRecognizer) {
    println("tapped")
}*/

}

and here is the method that returns the view in the view controller:

func mapView(mapView: GMSMapView!, markerInfoWindow marker: GMSMarker!) -> UIView! {
    println("markeInfoWindow")
    var newView = NSBundle.mainBundle().loadNibNamed("MarkerWindowView", owner: self, options: nil).first! as! MarkerWindowView
    //println(newView.description)
    newView.userName.text = "Kazuma"
    newView.videoLocation.text = "Harvard"
    newView.tags.text = "Kazuma is crazy"
    //view.bringSubviewToFront(newView)
    //view.insertSubview(newView, atIndex: 0)
    //newView.userInteractionEnabled = true
    /*for subView in self.view.subviews {
        println(subView)
    }*/
    //view.bringSubviewToFront(newView)
    return newView
}

I have tried a bunch of stuff to try to interact with this view including shifting views in the stack, moving layers etc. Has anyone had this problem before? Any idea what I'm doing wrong. The stuff I commented out is mostly me trying to hack it.

回答1:

A custom info window is rendered as an UIImage, so you cannot interact with anything on it. I had a similar problem, and ended up making the entire window one "button" that caused an embed segue to push a view over the map, giving me details (think Yelp app style).

My info window was contained in a separate .xib. Here is some code in my GMSMapViewDelegate:

func mapView(mapView: GMSMapView!, didTapInfoWindowOfMarker marker: GMSMarker!)
{
    ShowDetailView(mapMarkerManager!.GetMapMarker(marker)!)
}

func mapView(mapView: GMSMapView!, markerInfoWindow marker: GMSMarker!) -> UIView!
{
    let markerInfoWindow = UINib(nibName: "MarkerInfoPopup", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as! MarkerInfoPopup

    markerInfoWindow.setValues(mapMarkerManager!.GetMapMarker(marker)!)
    return markerInfoWindow
}

private func ShowDetailView(mapMarker: MapMarker)
{
    delegate!.SetMapMarker(self, mapMarker: mapMarker)

    self.view.bringSubviewToFront(mapDetailView)
    mapDetailView.hidden = false

    mapView.hidden = true
    BackToMapButton.hidden = false
}

(mapMarker is more or less a GMSMarker wrapper. It does other stuff but is not important in this context)

I know this isn't exactly what you wanted, but unfortunately there aren't many options when using Google Maps' built in markerInfoWindow/didTapInfoWindowOfMarker.

Reference: https://developers.google.com/maps/documentation/ios-sdk/marker?hl=en#info_windows

"Note: The info window is rendered as an image each time it is displayed on the map. This means that any changes to its properties while it is active will not be immediately visible. The contents of the info window will be refreshed the next time that it is displayed."