代码之家  ›  专栏  ›  技术社区  ›  Ahmed

在google maps ios sdk中获取两点之间的方向

  •  1
  • Ahmed  · 技术社区  · 6 年前

    我有以下代码来获取google maps 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)
        }
    }
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   rmaddy    6 年前

    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()