To solve this problem, we need two methods together, so I combined them in this way, I hope this helps in this matter:
func mapView(_ mapView: GMSMapView, didTap overlay: GMSOverlay) {
print(overlay)
}
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
print(coordinate)
for polyline in polylines {
if GMSGeometryIsLocationOnPath(coordinate, polyline.path!, true) {
self.mapView(mapView, didTap: polyline)
}
}
for polygon in polygons {
if GMSGeometryContainsLocation(coordinate, polygon.path!, true) {
self.mapView(mapView, didTap: polygon)
}
}
}
if the user clicked on coordinate, we can handle this. Then check if this one is contained coordinatein any Polylineor Polygonthat we defined earlier. So, we are an fireevent didTap overlayfor this overlay.
Be sure to do polylinesandpolygons isTappable = false
, overlay , overlaped, return, if overlay