How to get Google places marker latitude and longitude in swift

Viewed 698

I tried to get marker location from didTapAt but i do not get the place id Later i have to fetch place photos and place details. Is there any way to get the exact place id from google markers?

func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
    print("You tapped at \(coordinate.latitude), \(coordinate.longitude)")
    mapView.camera = GMSCameraPosition.camera(withLatitude: coordinate.latitude, longitude: coordinate.longitude, zoom: 15)
}

var placesClient: GMSPlacesClient!
let fields: GMSPlaceField = GMSPlaceField(rawValue:UInt(GMSPlaceField.name.rawValue) |
        UInt(GMSPlaceField.placeID.rawValue) |
        UInt(GMSPlaceField.coordinate.rawValue) |
        GMSPlaceField.addressComponents.rawValue |
        GMSPlaceField.rating.rawValue |
        GMSPlaceField.photos.rawValue |
        GMSPlaceField.all.rawValue |
        GMSPlaceField.formattedAddress.rawValue)!

    placesClient?.fetchPlace(fromPlaceID: placeID, placeFields: fields, sessionToken: nil, callback: {
        (place: GMSPlace?, error: Error?) in
        if let error = error {
            print("An error occurred: \(error.localizedDescription)")
            self.stopActivityIndicator()
            return
        }
})

enter image description here

1 Answers

Under the GMSMapViewDelegate there is a function that listens for taps on point of interest locations. The function func mapView(_ mapView: GMSMapView, didTapPOIWithPlaceID placeID: String, name: String, location: CLLocationCoordinate2D) returns location information as well as the place identifier.

Please see the following example from the Google Maps iOS SDK documentation.

Related