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

标签 swift google-maps swift3 google-api google-distancematrix-api

这里我传递了源和目的地的纬度和经度,我得到了响应,但如何从响应中获取特定值。我是 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/

相关文章:

ios - 在没有 StoryBoard 的情况下通过 Swift 中的 View Controller 以编程方式传递变量

android - URL 类呈现 - Invalid_Request with next_page_token

ios - NSPathStore2 的 UIImageView 实例泄露

具有完美 : Add a scheduled timer with interval to the runLoop 的 Swift 3 Linux

swift - Swift 3 中的对齐内存

swift - 点击单元格后在第二个 UI Collection View 中加载特定项目?

swift - 如何在 Swift 中为文件访问指定备用目录

ios - 如何将选定的 UIPickerView 行元素与数组元素进行比较?

android - 如何在 Android google map V2 api 上更改语言

android - 如何向MapActivity添加菜单?