如何获取用户点击位置的经纬度?在 Swift v3 和 google 地图 sdk 中

How do I get the Lat Long of where the user tapped? in Swift v3 and google maps sdk

我正在用 Swift3 编写一个应用程序。我正在使用 google 地图 SDK。我已经实现了 GMSMapView。我试图确定用户在 GMSMapView 中点击了多长时间。

下面是 android 开发版本的参考资料,我认为它会有所帮助。但是我找不到 iOS.

的等价物

https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap.OnMapClickListener

编辑:"possible duplicate" 没有解决我的问题。

我在以下位置找到了解决方案:https://developers.google.com/maps/documentation/ios-sdk/events 通过谷歌搜索 didTapAtCoordinate,我不知何故知道它是 SDK 的一部分。

对我来说最重要的一行是:mapView.delegate = self。如果我使用 mapView.delegate = self,我尝试过的其他一些解决方案可能会奏效,但没有其他人提到它。此外,func mapView 当然很重要,因此我可以使用用户点击位置的坐标。

import UIKit
import GoogleMaps

override func loadView() {
  let camera = GMSCameraPosition.camera(withLatitude: 1.285,
                                        longitude: 103.848,
                                        zoom: 12)

  let mapView = GMSMapView.map(withFrame: .zero, camera: camera)
  mapView.delegate = self
  self.view = mapView
}

// MARK: GMSMapViewDelegate
class DemoViewController: UIViewController, GMSMapViewDelegate {
}

func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
        print("You tapped at \(coordinate.latitude), \(coordinate.longitude)")
}//end func