-5

I'm coding an app in Swift3. I'm using the google maps sdk. I've implemented a GMSMapView. I'm trying to ascertain what lat long that the user tapped in the GMSMapView.

Below is the reference for the android development version of what I think would help. But I can't find the equivalent for iOS.

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

edit: The "possible duplicate" didn't solve my problem.

Neo42
  • 642
  • 3
  • 10
  • 31
  • 3
    Possible duplicate of [How to get Lat Long of tapped location in Google Map iOS Swift](https://stackoverflow.com/questions/38530470/how-to-get-lat-long-of-tapped-location-in-google-map-ios-swift) – nathan Aug 11 '17 at 04:32
  • It's not a duplicate because I'm talking about Swift version 3 and the latest version of google maps SDK. I tried the solution in the possible duplicate you mentioned but it never seems to trigger. – Neo42 Aug 11 '17 at 13:32
  • 4
    The API is indeed the same: [GMSMapViewDelegate](https://developers.google.com/maps/documentation/ios-sdk/reference/protocol_g_m_s_map_view_delegate-p.html#a9f226252840c79a996df402da9eec235). Have you read the comment below the answer? *Looking at the documentation yes, this should work for Swift 3. Do you have your GMSMapView delegate set?* – nathan Aug 11 '17 at 14:10
  • Now. Looking at your comment, I understand what you're asking. "Do you have your MapView delegate set?" You're right. I didn't. But I didn't know what to set it to or what you meant. Some aspects of hooking in controls and UI can be easy to forget. – Neo42 Aug 11 '17 at 14:44
  • 1
    If I may make a suggestion, if you didn't know how to do or what was meant, that's a perfect time to ask a question in a followup comment. This is a Q&A site after all. ;-) – BergQuester Aug 11 '17 at 14:49

1 Answers1

0

I found the solution at: https://developers.google.com/maps/documentation/ios-sdk/events by googling for didTapAtCoordinate, which I somehow knew was part of the SDK.

The most important line for me was: mapView.delegate = self. It's possible that some other solutions I tried would have worked had I used mapView.delegate = self, but nothing else had mentioned it. Also, the func mapView is of course important so I can use the coordinates of where the user tapped.

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
Neo42
  • 642
  • 3
  • 10
  • 31