所以我使用谷歌地图SDK,谷歌方向API基本上获取方向的JSON数据并将其绘制出来。那里的一切都很棒。 我正在使用GMSAutocompleteViewController类来使一切变得非常快速和简单。
我也(或者我想尝试)通过带有JSON的Google Places API以编程方式将地点添加到我的特定设备。一切正常,我从JSONResponse获得OK状态。
我知道,一旦发生这种情况,我应该立即将所述地点添加到用户的Google数据库(也就是我的设备和我的设备的谷歌数据库)中。话虽这么说,当我转到我的搜索按钮并弹出自动完成功能时,我输入我指定名称参数的名称,因为它没有显示。
现在我一直在查看地点api(autocompleteviewcontroller),看看是否有办法做某种涉及像viewcontroller.updateAllGMSPLaces或者荒谬的事情的功能,但我无法想到什么或找到任何东西到目前为止对不起,我整天都这样做,所以我已经烧坏了,我为这里可能存在的愚蠢而道歉。
我不包括当我实际绘制路线时如何处理JSONResponse,因为我觉得这与此无关(再次,我可能是错的)
我在viewDidLoad函数中添加了POST请求:
// 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()
我的自动完成视图控制器功能的按钮监听器:
//searching button
@IBAction func autocompleteClicked(_ sender: UIButton) {
let autocompleteController = GMSAutocompleteViewController()
autocompleteController.delegate = self
present(autocompleteController, animated: true, completion: nil)
}
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
}