2017-08-10 41 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