Ios 当使用google directions Api并请求json中的数据时,获取零数据
我对使用swift进行编程和ios开发还不熟悉。我试图在两个位置之间绘制路线,但遇到了此问题Ios 当使用google directions Api并请求json中的数据时,获取零数据,ios,json,swift,google-maps,Ios,Json,Swift,Google Maps,我对使用swift进行编程和ios开发还不熟悉。我试图在两个位置之间绘制路线,但遇到了此问题 func getDirections(origin: String!, destination: String!, waypoints: Array<String>!, travelMode: AnyObject!, completionHandler: ((status: String, success: Bool) -> Void)) { if let originLoca
func getDirections(origin: String!, destination: String!, waypoints: Array<String>!, travelMode: AnyObject!, completionHandler: ((status: String, success: Bool) -> Void)) {
if let originLocation = origin {
if let destinationLocation = destination {
var directionsURLString = baseURLDirections + "origin=" + originLocation + "&destination=" + destinationLocation
directionsURLString = directionsURLString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
let directionsURL = NSURL(string: directionsURLString)
dispatch_async(dispatch_get_main_queue(), { () -> Void in
let directionsData = NSData(contentsOfURL: directionsURL!)
println(directionsData)
var error: NSError?
let dictionary: Dictionary<NSObject, AnyObject> = NSJSONSerialization.JSONObjectWithData(directionsData!, options: NSJSONReadingOptions.MutableContainers, error: &error) as! Dictionary<NSObject, AnyObject>
您没有在请求URL中包含
API键
您可以使用NSURLSession
执行方向API请求。从API响应返回数据时,可以使用NSJSONSerialization.JSONObjectWithData()
解析JSON日期
func directionAPITest() {
let directionURL = "https://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&key=YOUR_API_KEY"
let request = NSURLRequest(URL: NSURL(string:directionURL)!)
let session = NSURLSession.sharedSession()
session.dataTaskWithRequest(request,
completionHandler: {(data: NSData!, response: NSURLResponse!, error: NSError!) in
if error == nil {
let object = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil) as! NSDictionary
println(object)
let routes = object["routes"] as! [NSDictionary]
for route in routes {
println(route["summary"])
}
dispatch_async(dispatch_get_main_queue()) {
//update your UI here
}
}
else {
println("Direction API error")
}
}).resume()
}
如果您需要自定义
mapView
或UITableView
,可以在dispatch\u async(dispatch\u get\u main\u queue()中执行此操作
关闭。您没有在请求的URL
中包含服务器密钥
而不是API密钥…创建一个服务器密钥并等待大约10分钟激活。您在API请求URL中使用的源
和目的地
是什么?'origin'和'destination'是正在ViewController中输入地址。为此,我们在向变量发送nil数据的UItextFields中发现了错误
func directionAPITest() {
let directionURL = "https://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&key=YOUR_API_KEY"
let request = NSURLRequest(URL: NSURL(string:directionURL)!)
let session = NSURLSession.sharedSession()
session.dataTaskWithRequest(request,
completionHandler: {(data: NSData!, response: NSURLResponse!, error: NSError!) in
if error == nil {
let object = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil) as! NSDictionary
println(object)
let routes = object["routes"] as! [NSDictionary]
for route in routes {
println(route["summary"])
}
dispatch_async(dispatch_get_main_queue()) {
//update your UI here
}
}
else {
println("Direction API error")
}
}).resume()
}