13

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?

Recusiwe
  • 1,594
  • 4
  • 31
  • 54

2 Answers2

13

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
polyline.zIndex = 0
polyline.map = 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(overlay.zindex)
  print("User Tapped Layer: \(overlay)")
}
Ayush Bansal
  • 103
  • 9
eshirima
  • 3,837
  • 5
  • 37
  • 61
4

You can use isTappable property of GMSPolyline.

isTappable

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 https://developers.google.com/maps/documentation/ios-sdk/reference/protocol_g_m_s_map_view_delegate-p.html#a3a2bf2ff4481528f931183cb364c0f4b

Community
  • 1
  • 1
PGDev
  • 23,751
  • 6
  • 34
  • 88