gpt4 book ai didi

swift - 如何在 swift 3.0 中从 google api 获取时间值

转载 作者:行者123 更新时间:2023-11-30 11:23:15 25 4
gpt4 key购买 nike

这里我传递了源和目的地的纬度和经度,我得到了响应,但如何从响应中获取特定值。我是 ios 开发人员的新手。我像这样在 google API 中传递纬度和经度

  let travelTimeString : NSString = "https://maps.googleapis.com/maps/api/directions/json?origin=19.120912,72.845121&destination=19.110162,72.853951&sensor=false" as NSString

print("travelTimeString",travelTimeString)
let urlStr = travelTimeString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
let searchURL: NSURL = NSURL(string: urlStr! as String)!
do {
let newdata = try Data(contentsOf: searchURL as URL)
if let responseDictionary = try JSONSerialization.jsonObject(with: newdata, options: []) as? NSDictionary {
print("travelTimeString",responseDictionary)


}} catch {
}

i got response like this那么如何获取持续时间的值(routes-->legs-->duration)

最佳答案

您可以使用任何外部库,或手动解析,例如:

 if let routes = responseDictionary["routes"] as? NSDictionary {
if let legs = routes["legs"] as? NSDictionary {
// etc
}
}

关于swift - 如何在 swift 3.0 中从 google api 获取时间值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51059339/

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