Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.3k views
in Technique[技术] by (71.8m points)

swift - Get LAT and LONG from tapped overlay in Google Maps

When a user taps an overlay, the following code is triggered:

func mapView(_ mapView: GMSMapView, didTap overlay: GMSOverlay) {


    }

I wonder if we can extract the exact LAT and LONG coordinates of the overlay that has been tapped?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...