So I am using Google Maps SDK, google directions API to essentially fetch JSON data for directions and drawing it out. Everything works great there. I am using GMSAutocompleteViewController class for this to make everything super quick and easy.
I am also (or I guess trying to) programmatically add places to my specific device through Google Places API with JSON. Everything is working, I am getting the OK status from the JSONResponse.
I know as soon as that occurs I should immediately have said place added to the user's Google database (aka my device and my device's google database). That being said, when I then go to my search button and the autocomplete functionality pops up and I type in the name that I assigned the name parameter as it doesn't show up.
Now i've been looking at the places api (autocompleteviewcontroller) to see if there is a way to do some sort of functionality that involves something like theviewcontroller.updateAllGMSPLaces or something ridiculous but I can't think of anything or find anything so far. I'm sorry i've been doing this all day so i'm burning out and I apologize for the stupidity that there might be here.
I'm not including how I handle the JSONResponse when i'm actually drawing out the routes as I feel that that is irrelevant for this (again, I could be wrong)
My adding place POST request in my viewDidLoad func:
// prepare json data
let json: [String: Any] = ["location": ["lat": mylat, "lng": mylong], "accuracy": 20, "name": "mumbo jump wumbo", "types": ["accounting"], "language": "en-US"]
let jsonData = try? JSONSerialization.data(withJSONObject: json)
// create post request
let url = URL(string: urlString)!
var request = URLRequest(url: url)
request.httpMethod = "POST"
// insert json data to the request
request.httpBody = jsonData
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print(error?.localizedDescription ?? "No data")
return
}
let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
if let responseJSON = responseJSON as? [String: Any] {
print(responseJSON)
}
}
task.resume()My button listener for autocompleteviewcontroller functionality:
//searching button
@IBAction func autocompleteClicked(_ sender: UIButton) {
let autocompleteController = GMSAutocompleteViewController()
autocompleteController.delegate = self
present(autocompleteController, animated: true, completion: nil)
}The autocompleteviewController:
extension ViewController: GMSAutocompleteViewControllerDelegate {
// Handle the user's selection.
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
//print("Place name: \(place.name)")
//print("Place address: \(place.formattedAddress)")
//print("Place attributions: \(place.attributions)")
//pass back long/lang to main class to do directions
self.directionsTo(searchedLatitude: place.coordinate.latitude, searchedLongitude: place.coordinate.longitude)
dismiss(animated: true, completion: nil)
}
func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
// TODO: handle the error.
print("Error: ", error.localizedDescription)
}
// User canceled the operation.
func wasCancelled(_ viewController: GMSAutocompleteViewController) {
dismiss(animated: true, completion: nil)
}
// Turn the network activity indicator on and off again.
func didRequestAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
UIApplication.shared.isNetworkActivityIndicatorVisible = true
}
func didUpdateAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}