Search code examples
swiftgoogle-maps-sdk-ios

Show GMSPolyline Title On Tap


I'm using Google Maps SDK for an iOS application of mine.

I configured it so that when a tap occurs on a GMSMarker, it shows it's title. It's working alright.

I want to do the same for GMSPolylines but couldn't figure out how. The lines's tappable property is set to true.

How can I achieve this. Thanks.

P.S. coding with Swift 2.0


Solution

  • Conform to the protocol GMSMapViewDelegate:

    class ViewController: UIViewController, GMSMapViewDelegate {
      ...
    }
    

    Set the ViewController to be the delegate of your map view:

    override func viewDidLoad() {
      ...
      viewMap.delegate = self
    }
    

    Use the method mapView:didTapOverlay:

    func mapView(mapView: GMSMapView, didTapOverlay overlay: GMSOverlay) {
      print(overlay.title)
    }
    

    This method is called whenever a tap is detected inside the map view.

    For more information: GMSOverlay