Search code examples
iosswiftgoogle-mapspolygonpolyline

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?


Solution

  • 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)")
    }