在这里我传递源和目标位置的纬度和经度,我得到了响应,但如何从响应中获取特定值。我是ios开发人员的新手,我在谷歌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 {
}
我得到了这样的回应 那么如何获取持续时间的值(路线-->腿-->持续时间(
您可以使用任何外部库,也可以手动解析,例如:
if let routes = responseDictionary["routes"] as? NSDictionary {
if let legs = routes["legs"] as? NSDictionary {
// etc
}
}