Save nearest establishment to google user account

2019-09-12 06:30发布

问题:

The purpose for this portion of the app is for the user (signed in with Google sign in) to locate the nearest place to them and save it automatically to the user's google account.

The below method works fine, grabbing the most likely place the user is located at (first object in the array of placeLikelihoods). Now what I need to do is save it to the user's google account (the one they signed in with).

Any lead or ideas on how to do this or if it's even possible? Thank you for the help like always.

func getInfoViaPlacesClient() {

let placesClient = GMSPlacesClient.sharedClient()

placesClient.currentPlaceWithCallback { (likelihoodlist, error) -> Void in

    if error != nil {
        print("Current Place error: \(error!.localizedDescription)")
        return
    }

    let nearestPlace : GMSPlaceLikelihood = likelihoodlist!.likelihoods.first as! GMSPlaceLikelihood
        print(nearestPlace.likelihood)
        self.nearestPlace = nearestPlace.place

        self.saveNearestPlace()

  }   
}

Save method... I think I'm on the right track but I keep getting this error in the placesClient.addPlace block...

Add Place error: The operation couldn’t be completed. (com.google.places.server.ErrorDomain error -1.)

func saveNearestPlace() {

//to app(core data)
let placesClient = GMSPlacesClient.sharedClient()

let name = self.nearestPlace!.name
let address = self.nearestPlace!.formattedAddress

LocationController.sharedInstance.addLocationWithName(name!, address: address!)

//to google 

let addedPlace = GMSUserAddedPlace()
addedPlace.name = self.nearestPlace?.name
addedPlace.coordinate = (self.nearestPlace?.coordinate)!
addedPlace.types = self.nearestPlace?.types
addedPlace.address = self.nearestPlace?.formattedAddress

placesClient.addPlace(addedPlace, callback: { (place: GMSPlace?, error: NSError?) -> Void in

    if let error = error {
        print("Add Place error: \(error.localizedDescription)")
        return
    }

    if let place = place {

        self.nearestPlace = place

        print("Added place with placeID \(place.placeID)")
        print("Added Place name \(place.name)")
        print("Added Place address \(place.formattedAddress)")
    }
})

}