3

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
jh314
  • 27,144
  • 16
  • 62
  • 82
altexo
  • 201
  • 3
  • 15

3 Answers3

8

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
}
altexo
  • 201
  • 3
  • 15
  • 1
    How do you call this function ? – Dipen Gajjar Oct 27 '16 at 10:44
  • 1
    @dipenbaks You must firsts make the Class implement GMSMapViewDelegate and than assigm mapView.delegate = self. Than the function will be called every time you press the marker like so: http://stackoverflow.com/a/16414971/774972 – Xitcod13 Nov 22 '16 at 07:20
4

You can use these steps for swift 4 and 5

  1. implement GMSMapViewDelegate

  2. punch your map to this delegate in didviewload() like this : mygooglemap.delegate = self

  3. add this function to your class:

     func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
     //do what ever you want
     return true
     }
    
iman kazemayni
  • 1,255
  • 1
  • 19
  • 20
2

For Swift 5+

Your Can Do This Using Delegete method

  1. Assign your mapview delete to self

  2. Confrim GMSMapViewDelegateto your self

Here is Code

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
    }
}
Ekramul Hoque
  • 672
  • 4
  • 17