Get LAT and LONG from tapped overlay in Google Maps

前端 未结 3 1214
耶瑟儿~
耶瑟儿~ 2020-12-12 05:05

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

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


    }

I w

3条回答
  •  情话喂你
    2020-12-12 05:32

    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

提交回复
热议问题