gpt4 book ai didi

ios - 在谷歌地图 iOS SDK 中获取两点之间的路线

转载 作者:搜寻专家 更新时间:2023-11-01 07:01:09 26 4
gpt4 key购买 nike

我有以下代码来获取 Google map iOS SDK 中两点之间的路径。但是,我没有收到任何返回的数据,甚至没有收到任何错误。

let url = URL(string: "http://maps.googleapis.com/maps/api/directions/json?origin=\(latitude),\(longitude)&destination=\(finallat),\(finallong)&key=**************")
URLSession.shared.dataTask(with: url!) { (data:Data?, response:URLResponse?, error:Error?) in
if let data = data {
do {
// Convert the data to JSON
let jsonSerialized = try JSONSerialization.jsonObject(with: data, options: []) as? [String : Any]

if let json = jsonSerialized, let url = json["url"], let explanation = json["explanation"] {
print(url)
print(explanation)
}
} catch let error as NSError {
print(error.localizedDescription)
}
} else if let error = error {
print(error.localizedDescription)
}
}

最佳答案

您没有对 dataTask 做任何事情,所以它实际上并没有被调用。您需要调用 resume()

let url = URL(string: "http://maps.googleapis.com/maps/api/directions/json?origin=\(latitude),\(longitude)&destination=\(finallat),\(finallong)&key=**************")
let task = URLSession.shared.dataTask(with: url!) { (data:Data?, response:URLResponse?, error:Error?) in
if let data = data {
do {
// Convert the data to JSON
let jsonSerialized = try JSONSerialization.jsonObject(with: data, options: []) as? [String : Any]

if let json = jsonSerialized, let url = json["url"], let explanation = json["explanation"] {
print(url)
print(explanation)
}
} catch let error as NSError {
print(error.localizedDescription)
}
} else if let error = error {
print(error.localizedDescription)
}
}
task.resume()

关于ios - 在谷歌地图 iOS SDK 中获取两点之间的路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51214352/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com