Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.6k 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 Reply

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...