2017-08-10 44 views
-5

我在Swift3中编写了一个应用程序。我正在使用Google地图sdk。我已经实现了一个GMSMapView。我试图确定用户在GMSMapView中点击了多长时间。如何获得用户点击的Lat Long?在Swift v3和谷歌地图sdk

下面是我认为会有所帮助的Android开发版本的参考。但我无法找到iOS的等价物。

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

编辑: “可能重复” 并没有解决我的问题。

+3

的可能的复制[如何获得在谷歌地图iOS版雨燕抽头位置的纬度龙(https://stackoverflow.com/questions/38530470/how-to-get-lat-long-of-tapped -location-in-google-map-ios-swift) – nathan

+0

这不是重复的,因为我正在谈论Swift版本3和谷歌地图SDK的最新版本。我试着在你提到的可能重复的解决方案,但它似乎从来没有触发。 – Neo42

+4

API确实是一样的:[GMSMapViewDelegate](https://developers.google.com/maps/documentation/ios-sdk/reference/protocol_g_m_s_map_view_delegate-p.html#a9f226252840c79a996df402da9eec235)。您是否阅读过答案下面的评论? *查看文档是的,这应该适用于Swift 3.你有你的GMSMapView委托集吗?* – nathan

回答

0

我找到了解决方案: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