Detect tap on GMSPolyline in Swift?

2 Solutions Collect From Internet About “Detect tap on GMSPolyline in Swift?”

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.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("User Tapped Layer: \(overlay)")
}

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