I am trying to implement a method that uses Googles snap to roads API however I have been unable to achieve any results.
I was able to successfully implement the directions API in to my Swift project using
https://maps.googleapis.com/maps/api/directions/json?origin=xyz
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()
however if the GPS coordinates are even slightly different I get an entirely different result.
What I am trying to do is plot a users path the same each time even if the start/end coordinates a slightly different.
The error I am getting is
fatal error: unexpectedly found nil while unwrapping an Optional value
Any suggestions?
Thanks
EDIT:
I am trying this but this is what is causing the error
func directionAPITest() {
let directionURL = "https://roads.googleapis.com/v1/snapToRoads?path=-35.27801,149.12958|-35.28032,149.12907|-35.28099,149.12929|-35.28144,149.12984|-35.28194,149.13003|-35.28282,149.12956|-35.28302,149.12881|-35.28473,149.12836&interpolate=true&key=xyz"
let request = NSURLRequest(URL: NSURL(string:directionURL)!)
let session = NSURLSession.sharedSession()
}