Google maps api 3 为谷歌地图标记分配动作点击,swift 2

Google maps api 3 为谷歌地图标记分配动作点击,swift 2,google-maps-api-3,swift2,ios9,xcode7,Google Maps Api 3,Swift2,Ios9,Xcode7,我有一个谷歌地图标记,我希望当我点击该标记时,将我发送到另一个viewController,或在我的地图上显示一个按钮 let marker1 = GMSMarker() marker1.position = CLLocationCoordinate2DMake(24.8236423, -107.4234671) marker1.appearAnimation = kGMSMarkerAnimationPop marker1.icon = UIImage(named: "flag_icon")

我有一个谷歌地图标记,我希望当我点击该标记时,将我发送到另一个viewController,或在我的地图上显示一个按钮

let marker1 = GMSMarker()
marker1.position = CLLocationCoordinate2DMake(24.8236423, -107.4234671)
marker1.appearAnimation = kGMSMarkerAnimationPop
marker1.icon = UIImage(named: "flag_icon")
marker1.title = "Any title"
marker1.snippet = "Any text"
marker1.map = mapView

我解决了,这是tap函数

    //Market Tap Function
func mapView(mapView: GMSMapView!, didTapMarker marker: GMSMarker!) -> Bool {
    let myFirstButton = UIButton()
    myFirstButton.setTitle("✸", forState: .Normal)
    myFirstButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
    myFirstButton.frame = CGRectMake(15, -50, 300, 500)
    myFirstButton.addTarget(self, action: "pressed:", forControlEvents: .TouchUpInside)

    self.view.addSubview(myFirstButton)
    return true
}

您可以将这些步骤用于swift 4和5

  • 实现GMSMapViewDelegate

  • 在didviewload()中按如下方式将映射穿入此委托:mygooglemap.delegate=self

  • 将此函数添加到类中:

     func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
     //do what ever you want
     return true
     }
    
  • 斯威夫特5号+

    您可以使用Delegete方法来实现这一点

  • 将地图视图删除指定给self

  • 与自己交谈
    GMSMapViewDelegate

  • 这是代码

    1

    import UIKit
    import GoogleMaps
    import GooglePlaces
    
    class HomeVC: UIViewController {
     
    override func viewDidLoad() {
         super.viewDidLoad()
         self.mapView.delegate = self
      }
    
    }
    
    
    2

    extension HomeVC:GMSMapViewDelegate {
        
        func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
            print("Do what ever you want.")
            return true
        }
    }
    

    如何调用此函数?@dipenbaks必须首先使类实现GMSMapViewDelegate,而不是assigm mapView.delegate=self。然后,每次按下标记时都会调用该函数,如下所示: