Search code examples
google-maps-api-3swift2ios9xcode7

Assign action tap to google map marker, swift 2


I have a google map marker and i want that when i tap the marker send me to another viewController, or display a button on my map,

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

Solution

  • I solved it, this is the tap function

        //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
    }