Get LAT and LONG from tapped overlay in Google Maps

前端 未结 3 1212
耶瑟儿~
耶瑟儿~ 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:34

    If you just want to get the exact coordinates wherever you tapped whether on Overlay or not, then there is another delegate of GMSMapViewDelegate which is called whenever we tap on GoogleMaps. In this delegate you can get the exact coordinates where you tapped on map irrespective of tapping on an overlay.

    Swift 3.0

     func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
            print(coordinate.latitude)
            print(coordinate.longitude)
        }
    

    If you want to get the coordinate only on tapping marker, then use this delegate method

    func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
            print(marker.position.latitude)
            print(marker.position.longitude)
            return true
        }
    

    Make sure to make your overlay as non-tappable

    overlay.isTappable = false
    

    For reference see here

提交回复
热议问题