Adding Custom Info Window to Google Maps

末鹿安然 提交于 2020-01-01 07:24:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!