To solve this we need the 2 methods together,
So I combined them in a way I hope it will help in this issue:
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 at coordinate
we will deal with this, Then check if this coordinate
is contained in any Polyline
or Polygon
we had defined before, So we fire
the event didTap overlay
for that overlay
.
Make sure to make polylines
and polygons
isTappable = false
And but in mind this event will be fired for every overlay
tapped even though if they are overlaped
, You can put return
when the if
success to take the first overlay
only
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…