Detect tap on GMSPolyline in Swift?


I'm struggling with detecting a tap on a GMSPolyline drawn on my Google map, it works just fine with GMSpolygones, but the same approach doesn't seem to work with polyline. My current approach, which works for polygones, is:

if (GMSGeometryContainsLocation(coordinate, polygon.path!, false)) {

Any suggestions how to detect taps on a polyline? Or just close to it?

Show source
| swift   | ios   | google-maps   | polygon   | polyline   2016-11-22 19:11 2 Answers

Answers ( 2 )

  1. 2016-11-25 02:11

    According to their API documentation, GMSPolyline inherits from GMSOverlay which means a GMSPolyline has the property tappable. So you'd want something like this

    let polyLine: GMSPolyline = GMSPolyline(path: newPath)
    polyLine.isTappable = true = yourGoogleMap

    Then your GMSMapViewDelegate should notify you of the tap anywhere within the GMSPolyline layer with this function

    func mapView(_ mapView: GMSMapView, didTap overlay: GMSOverlay)
      print("User Tapped Layer: \(overlay)")
  2. 2016-11-25 03:11

    You can use isTappable property of GMSPolyline.


    If this overlay should cause tap notifications.

    polyline.isTappable = true

    GMSPolyline inherits from GMSOverlay. So to detect tap on overlays GMSMapViewDelegate provides a delegate method:

    • mapView:didTapOverlay: Called after an overlay has been tapped.

    Whenever the polyline is tapped, the GMSMapViewDelegate method didTapOverlay is called

    func mapView(_ mapView: GMSMapView, didTap overlay: GMSOverlay) {
            //Write your code here

    Also, this method can be used for GMSPolygon since it also inherits from GMSOverlay.

    For further information refer

◀ Go back